2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1997, 1998
5 * Sleepycat Software. All rights reserved.
11 static const char sccsid
[] = "@(#)os_alloc.c 10.10 (Sleepycat) 10/12/98";
14 #ifndef NO_SYSTEM_INCLUDES
15 #include <sys/types.h>
27 * Correct for systems that return NULL when you allocate 0 bytes of memory.
28 * There are several places in DB where we allocate the number of bytes held
29 * by the key/data item, and it can be 0. Correct here so that malloc never
30 * returns a NULL for that reason (which behavior is permitted by ANSI). We
31 * could make these calls macros on non-Alpha architectures (that's where we
32 * saw the problem), but it's probably not worth the autoconf complexity.
35 * Correct for systems that don't set errno when malloc and friends fail.
38 * We wish to hold the whole sky,
44 * The strdup(3) function for DB.
46 * PUBLIC: int __os_strdup __P((const char *, void *));
49 __os_strdup(str
, storep
)
57 *(void **)storep
= NULL
;
59 size
= strlen(str
) + 1;
60 if ((ret
= __os_malloc(size
, NULL
, &p
)) != 0)
71 * The calloc(3) function for DB.
73 * PUBLIC: int __os_calloc __P((size_t, size_t, void *));
76 __os_calloc(num
, size
, storep
)
84 if ((ret
= __os_malloc(size
, NULL
, &p
)) != 0)
95 * The malloc(3) function for DB.
97 * PUBLIC: int __os_malloc __P((size_t, void *(*)(size_t), void *));
100 __os_malloc(size
, db_malloc
, storep
)
102 void *(*db_malloc
) __P((size_t)), *storep
;
106 *(void **)storep
= NULL
;
108 /* Never allocate 0 bytes -- some C libraries don't like it. */
112 /* Some C libraries don't correctly set errno when malloc(3) fails. */
114 if (db_malloc
!= NULL
)
116 else if (__db_jump
.j_malloc
!= NULL
)
117 p
= __db_jump
.j_malloc(size
);
127 memset(p
, 0xdb, size
);
129 *(void **)storep
= p
;
136 * The realloc(3) function for DB.
138 * PUBLIC: int __os_realloc __P((void *, size_t));
141 __os_realloc(storep
, size
)
147 ptr
= *(void **)storep
;
149 /* If we haven't yet allocated anything yet, simply call malloc. */
151 return (__os_malloc(size
, NULL
, storep
));
153 /* Never allocate 0 bytes -- some C libraries don't like it. */
158 * Some C libraries don't correctly set errno when realloc(3) fails.
160 * Don't overwrite the original pointer, there are places in DB we
161 * try to continue after realloc fails.
164 if (__db_jump
.j_realloc
!= NULL
)
165 p
= __db_jump
.j_realloc(ptr
, size
);
167 p
= realloc(ptr
, size
);
174 *(void **)storep
= p
;
181 * The free(3) function for DB.
183 * PUBLIC: void __os_free __P((void *, size_t));
192 memset(ptr
, 0xdb, size
);
195 if (__db_jump
.j_free
!= NULL
)
196 __db_jump
.j_free(ptr
);
203 * The free(3) function for DB, freeing a string.
205 * PUBLIC: void __os_freestr __P((void *));
212 memset(ptr
, 0xdb, strlen(ptr
) + 1);
215 if (__db_jump
.j_free
!= NULL
)
216 __db_jump
.j_free(ptr
);