1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
/*
* Copyright (c) 2017 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/errno.h>
#include <stdbool.h>
#include <string.h>
#include <strings.h>
#include <pthread.h>
#include <LongBow/runtime.h>
#include <parc/algol/parc_StdlibMemory.h>
static uint32_t _parcStdlibMemory_OutstandingAllocations;
#ifdef PARCLibrary_DISABLE_ATOMICS
static pthread_mutex_t _parcStdlibMemory_Mutex = PTHREAD_MUTEX_INITIALIZER;
static inline void
_parcStdlibMemory_IncrementOutstandingAllocations(void)
{
pthread_mutex_lock(&_parcStdlibMemory_Mutex);
_parcStdlibMemory_OutstandingAllocations++;
pthread_mutex_unlock(&_parcStdlibMemory_Mutex);
}
static inline void
_parcStdlibMemory_DecrementOutstandingAllocations(void)
{
pthread_mutex_lock(&_parcStdlibMemory_Mutex);
_parcStdlibMemory_OutstandingAllocations--;
pthread_mutex_unlock(&_parcStdlibMemory_Mutex);
}
#else
static inline void
_parcStdlibMemory_IncrementOutstandingAllocations(void)
{
__sync_add_and_fetch(&_parcStdlibMemory_OutstandingAllocations, 1);
}
static inline void
_parcStdlibMemory_DecrementOutstandingAllocations(void)
{
__sync_sub_and_fetch(&_parcStdlibMemory_OutstandingAllocations, 1);
}
#endif
#if HAVE_REALLOC == 0
static void *
_parcStdlibMemory_rplRealloc(void *oldAlloc, size_t newSize)
{
if (newSize == 0) {
newSize = 1;
}
char *newAlloc = malloc(newSize);
if (oldAlloc != NULL) {
memcpy(newAlloc, oldAlloc, newSize);
free(oldAlloc);
}
return newAlloc;
}
#endif
void *
parcStdlibMemory_Allocate(size_t size)
{
if (size == 0) {
return NULL;
}
void *result = malloc(size);
if (result != NULL) {
_parcStdlibMemory_IncrementOutstandingAllocations();
}
return result;
}
void *
parcStdlibMemory_AllocateAndClear(size_t size)
{
void *pointer = parcStdlibMemory_Allocate(size);
if (pointer != NULL) {
memset(pointer, 0, size);
}
return pointer;
}
int
parcStdlibMemory_MemAlign(void **pointer, size_t alignment, size_t size)
{
if (size == 0) {
return EINVAL;
}
int failure = posix_memalign(pointer, alignment, size);
if (failure != 0) {
return failure;
}
if (*pointer == NULL) {
return ENOMEM;
}
_parcStdlibMemory_IncrementOutstandingAllocations();
return 0;
}
void
parcStdlibMemory_Deallocate(void **pointer)
{
#ifndef PARCLibrary_DISABLE_VALIDATION
trapIllegalValueIf(_parcStdlibMemory_OutstandingAllocations == 0,
"parcStdlibMemory_Deallocate invoked with nothing left to free (double free somewhere?)\n");
#endif
free(*pointer);
*pointer = NULL;
_parcStdlibMemory_DecrementOutstandingAllocations();
}
void *
parcStdlibMemory_Reallocate(void *pointer, size_t newSize)
{
#if HAVE_REALLOC
void *result = realloc(pointer, newSize);
#else
void *result = _parcStdlibMemory_rplRealloc(pointer, newSize);
#endif
if (pointer == NULL) {
_parcStdlibMemory_IncrementOutstandingAllocations();
}
return result;
}
char *
parcStdlibMemory_StringDuplicate(const char *string, size_t length)
{
_parcStdlibMemory_IncrementOutstandingAllocations();
return strndup(string, length);
}
uint32_t
parcStdlibMemory_Outstanding(void)
{
return _parcStdlibMemory_OutstandingAllocations;
}
PARCMemoryInterface PARCStdlibMemoryAsPARCMemory = {
.Allocate = (uintptr_t) parcStdlibMemory_Allocate,
.AllocateAndClear = (uintptr_t) parcStdlibMemory_AllocateAndClear,
.MemAlign = (uintptr_t) parcStdlibMemory_MemAlign,
.Deallocate = (uintptr_t) parcStdlibMemory_Deallocate,
.Reallocate = (uintptr_t) parcStdlibMemory_Reallocate,
.StringDuplicate = (uintptr_t) parcStdlibMemory_StringDuplicate,
.Outstanding = (uintptr_t) parcStdlibMemory_Outstanding
};
|