4 Copyright (C) Andrew Tridgell 2004
6 ** NOTE! The following LGPL license applies to the ldb
7 ** library. This does NOT imply that all of Samba is released
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 2 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 * Component: ldb alloc
30 * Description: functions for memory allocation
32 * Author: Andrew Tridgell
39 this allows the user to choose their own allocation function
41 int ldb_set_alloc(struct ldb_context *ldb,
42 void *(*alloc)(void *context, void *ptr, size_t size),
45 ldb->alloc_ops.alloc = alloc;
46 ldb->alloc_ops.context = context;
51 this is the default memory allocation function
53 static void *ldb_default_alloc(void *context, void *ptr, size_t size)
55 /* by setting LDB_ALLOC_OFS to non-zero the test suite can
56 catch any places where we incorrectly use the libc alloc
58 #define LDB_ALLOC_OFS 4
59 /* we don't assume a modern realloc function */
61 ptr = malloc(size+LDB_ALLOC_OFS);
62 if (ptr) return ((char *)ptr)+LDB_ALLOC_OFS;
66 free(((char *)ptr)-LDB_ALLOC_OFS);
69 ptr = realloc(((char *)ptr)-LDB_ALLOC_OFS, size+LDB_ALLOC_OFS);
71 return ((char *)ptr)+LDB_ALLOC_OFS;
77 all memory allocation goes via this function
79 void *ldb_realloc(struct ldb_context *ldb, void *ptr, size_t size)
81 if (!ldb->alloc_ops.alloc) {
82 ldb_set_alloc(ldb, ldb_default_alloc, NULL);
84 return ldb->alloc_ops.alloc(ldb->alloc_ops.context, ptr, size);
87 void *ldb_malloc(struct ldb_context *ldb, size_t size)
89 return ldb_realloc(ldb, NULL, size);
92 void ldb_free(struct ldb_context *ldb, void *ptr)
95 ldb_realloc(ldb, ptr, 0);
99 void *ldb_strndup(struct ldb_context *ldb, const char *str, size_t maxlen)
101 size_t len = strnlen(str, maxlen);
103 ret = ldb_realloc(ldb, NULL, len+1);
105 memcpy(ret, str, len);
106 ((char *)ret)[len] = 0;
111 void *ldb_strdup(struct ldb_context *ldb, const char *str)
113 size_t len = strlen(str);
115 ret = ldb_realloc(ldb, NULL, len+1);
117 memcpy(ret, str, len+1);
123 a ldb wrapper for asprintf(), using ldb_malloc()
125 int ldb_asprintf(struct ldb_context *ldb, char **strp, const char *fmt, ...) _PRINTF_ATTRIBUTE(3, 4)
133 len = vsnprintf(NULL, 0, fmt, ap);
139 *strp = ldb_malloc(ldb, len+1);
145 len2 = vsnprintf(*strp, len+1, fmt, ap);
149 /* buggy (or non-C99) vsnprintf function */
150 ldb_free(ldb, *strp);
158 realloc an array, checking for integer overflow in the array size
160 void *ldb_realloc_array(struct ldb_context *ldb,
161 void *ptr, size_t el_size, unsigned count)
163 #define MAX_MALLOC_SIZE 0x7fffffff
166 count >= MAX_MALLOC_SIZE/el_size) {
170 return ldb_malloc(ldb, el_size * count);
172 return ldb_realloc(ldb, ptr, el_size * count);