File: memory.c

package info (click to toggle)
aolserver 3.4.2-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 22,692 kB
  • ctags: 33,612
  • sloc: ansic: 171,340; tcl: 10,218; sh: 3,821; cpp: 2,779; makefile: 2,041; yacc: 1,648; perl: 456; php: 13
file content (331 lines) | stat: -rw-r--r-- 6,455 bytes parent folder | download
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
 * The contents of this file are subject to the AOLserver Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://aolserver.com/.
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 *
 * The Original Code is AOLserver Code and related documentation
 * distributed by AOL.
 * 
 * The Initial Developer of the Original Code is America Online,
 * Inc. Portions created by AOL are Copyright (C) 1999 America Online,
 * Inc. All Rights Reserved.
 *
 * Alternatively, the contents of this file may be used under the terms
 * of the GNU General Public License (the "GPL"), in which case the
 * provisions of GPL are applicable instead of those above.  If you wish
 * to allow use of your version of this file only under the terms of the
 * GPL and not to allow others to use your version of this file under the
 * License, indicate your decision by deleting the provisions above and
 * replace them with the notice and other provisions required by the GPL.
 * If you do not delete the provisions above, a recipient may use your
 * version of this file under either the License or the GPL.
 */

/* 
 * memory.c --
 *
 *	Memory allocation routine wrappers which abort the process on error.
 */

static const char *RCSID = "@(#) $Header: /cvsroot/aolserver/aolserver/thread/memory.c,v 1.4 2000/11/06 17:53:50 jgdavidson Exp $, compiled: " __DATE__ " " __TIME__;

#include "thread.h"

int nsMemPools = 0; /* Should per-thread memory pools be used? This must
		     * be set before the first ns_malloc in a program. */


/*
 *----------------------------------------------------------------------
 *
 * NsAlloc, NsFree --
 *
 *	Simple allocator for thread objects.  These routines are used
 *	internally in the thread library to avoid any conflict with
 *	the pools.  This should not be a performance issue as allocating
 *	memory for these objects is somewhat rare, normally just at
 *	startup and thread create time.
 *
 * Results:
 *	NsAlloc: Pointer to zero'ed memory.
 *	NsFree: None.
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

void *
NsAlloc(size_t size)
{
    void *ptr;

    ptr = malloc(size);
    if (ptr == NULL) {
	NsThreadAbort("NsAlloc: could not allocate %d bytes", size);
    }
    memset(ptr, 0, size);
    return ptr;
}

void
NsFree(void *ptr)
{
    free(ptr);
}


/*
 *----------------------------------------------------------------------
 *
 * ns_realloc, ns_malloc, ns_calloc, ns_free, ns_strdup, ns_strcopy --
 *
 *	Memory allocation wrappers which either call the platform
 *	versions or the fast pool allocator for a per-thread pool.
 *
 * Results:
 *	As with system functions.
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

void *
ns_realloc(void *ptr, size_t size)
{
    if (ptr == NULL) {
	ptr = ns_malloc(size);
    } else if (nsMemPools) {
	ptr = Ns_ThreadRealloc(ptr, size);
    } else {
	ptr = realloc(ptr, size);
    }	
    if (ptr == NULL) {
	NsThreadAbort("ns_realloc: could not allocate %d bytes", size);
    }
    return ptr;
}

void *
ns_malloc(size_t size)
{
    void *ptr;

    if (nsMemPools) {
	ptr = Ns_ThreadMalloc(size);
    } else {
	ptr = malloc(size);
    }
    if (ptr == NULL) {
	NsThreadAbort("ns_malloc: could not allocate %d bytes", size);
    }
    return ptr;
}

void
ns_free(void *ptr)
{
    if (ptr != NULL) {
	if (nsMemPools) {
	    Ns_ThreadFree(ptr);
	} else {
	    free(ptr);
	}
    }
}

void *
ns_calloc(size_t num, size_t esize)
{
    void *new;
    size_t size;

    size = num * esize;
    new = ns_malloc(size);
    memset(new, 0, size);

    return new;
}

char *
ns_strcopy(char *old)
{
    return (old == NULL ? NULL : ns_strdup(old));
}

char *
ns_strdup(char *old)
{
    char *new;

    new = ns_malloc(strlen(old) + 1);
    strcpy(new, old);

    return new;
}


/*
 *----------------------------------------------------------------------
 *
 * Ns_ThreadPool --
 *
 *	Return the memory pool for the calling thread.
 *
 * Results:
 *	Pointer to Ns_Pool.
 *
 * Side effects:
 *	Pool is created if necessary.
 *
 *----------------------------------------------------------------------
 */

Ns_Pool *
Ns_ThreadPool(void)
{
    Thread *thisPtr = NsGetThread();

    if (thisPtr->pool == NULL) {
	thisPtr->pool = Ns_PoolCreate(thisPtr->name);
    }
    return thisPtr->pool;
}


/*
 *----------------------------------------------------------------------
 *
 * Ns_ThreadMalloc, Ns_ThreadRealloc, Ns_ThreadCalloc, Ns_ThreadFree,
 * Ns_ThreadStrDup, Ns_ThreadStrCopy --
 *
 *	Allocate/free memory from the per-thread pool.
 *
 * Results:
 *	See Ns_Pool routines.
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

void *
Ns_ThreadMalloc(size_t size)
{
    return Ns_PoolAlloc(Ns_ThreadPool(), size);
}    

void *
Ns_ThreadRealloc(void *ptr, size_t size)
{   
    return Ns_PoolRealloc(Ns_ThreadPool(), ptr, size);
}

void
Ns_ThreadFree(void *ptr)
{   
    Ns_PoolFree(Ns_ThreadPool(), ptr);
}

void *
Ns_ThreadCalloc(size_t nelem, size_t elsize)
{
    return Ns_PoolCalloc(Ns_ThreadPool(), nelem, elsize);
}

char *
Ns_ThreadStrDup(char *old)
{
    return Ns_PoolStrDup(Ns_ThreadPool(), old);
}

char *
Ns_ThreadStrCopy(char *old)
{
    return Ns_PoolStrCopy(Ns_ThreadPool(), old);
}


/*
 * Backward compatible wrappers.
 */

#ifdef Ns_Malloc
#undef Ns_Malloc
#endif

void *
Ns_Malloc(size_t size)
{
    return ns_malloc(size);
}

#ifdef Ns_Realloc
#undef Ns_Realloc
#endif

void *
Ns_Realloc(void *ptr, size_t size)
{
    return ns_realloc(ptr, size);
}

#ifdef Ns_Calloc
#undef Ns_Calloc
#endif

void *
Ns_Calloc(size_t nelem, size_t elsize)
{
    return ns_calloc(nelem, elsize);
}

#ifdef Ns_Free
#undef Ns_Free
#endif

void 
Ns_Free(void *ptr)
{
    ns_free(ptr);
}

#ifdef Ns_StrDup
#undef Ns_StrDup
#endif

char *
Ns_StrDup(char *str)
{
    return ns_strdup(str);
}

#ifdef Ns_StrCopy
#undef Ns_StrCopy
#endif

char *
Ns_StrCopy(char *str)
{
    return ns_strcopy(str);
}

#ifdef Ns_ThreadAlloc
#undef Ns_ThreadAlloc
#endif

void *
Ns_ThreadAlloc(size_t size)
{
    return Ns_ThreadMalloc(size);
}