r2055: Add PRINTF_ATTRIBUTE to many more parts of the code, and a new
[kamenim/samba.git] / source4 / lib / ldb / common / ldb_alloc.c
1 /* 
2    ldb database library
3
4    Copyright (C) Andrew Tridgell  2004
5
6      ** NOTE! The following LGPL license applies to the ldb
7      ** library. This does NOT imply that all of Samba is released
8      ** under the LGPL
9    
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.
14
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.
19
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
23 */
24
25 /*
26  *  Name: ldb
27  *
28  *  Component: ldb alloc
29  *
30  *  Description: functions for memory allocation
31  *
32  *  Author: Andrew Tridgell
33  */
34
35 #include "includes.h"
36
37
38 /*
39   this allows the user to choose their own allocation function
40 */
41 int ldb_set_alloc(struct ldb_context *ldb,
42                   void *(*alloc)(void *context, void *ptr, size_t size),
43                   void *context)
44 {
45         ldb->alloc_ops.alloc = alloc;
46         ldb->alloc_ops.context = context;
47         return 0;
48 }
49
50 /*
51   this is the default memory allocation function
52 */
53 static void *ldb_default_alloc(void *context, void *ptr, size_t size)
54 {
55         /* by setting LDB_ALLOC_OFS to non-zero the test suite can
56            catch any places where we incorrectly use the libc alloc
57            funcitons directly */
58 #define LDB_ALLOC_OFS 4
59         /* we don't assume a modern realloc function */
60         if (ptr == NULL) {
61                 ptr = malloc(size+LDB_ALLOC_OFS);
62                 if (ptr) return ((char *)ptr)+LDB_ALLOC_OFS;
63                 return NULL;
64         }
65         if (size == 0) {
66                 free(((char *)ptr)-LDB_ALLOC_OFS);
67                 return NULL;
68         }
69         ptr = realloc(((char *)ptr)-LDB_ALLOC_OFS, size+LDB_ALLOC_OFS);
70         if (ptr) {
71                 return ((char *)ptr)+LDB_ALLOC_OFS;
72         }
73         return NULL;
74 }
75
76 /*
77   all memory allocation goes via this function
78 */
79 void *ldb_realloc(struct ldb_context *ldb, void *ptr, size_t size)
80 {
81         if (!ldb->alloc_ops.alloc) {
82                 ldb_set_alloc(ldb, ldb_default_alloc, NULL);
83         }
84         return ldb->alloc_ops.alloc(ldb->alloc_ops.context, ptr, size);
85 }
86
87 void *ldb_malloc(struct ldb_context *ldb, size_t size)
88 {
89         return ldb_realloc(ldb, NULL, size);
90 }
91
92 void ldb_free(struct ldb_context *ldb, void *ptr)
93 {
94         if (ptr != NULL) {
95                 ldb_realloc(ldb, ptr, 0);
96         }
97 }
98
99 void *ldb_strndup(struct ldb_context *ldb, const char *str, size_t maxlen)
100 {
101         size_t len = strnlen(str, maxlen);
102         void *ret;
103         ret = ldb_realloc(ldb, NULL, len+1);
104         if (ret) {
105                 memcpy(ret, str, len);
106                 ((char *)ret)[len] = 0;
107         }
108         return ret;
109 }
110
111 void *ldb_strdup(struct ldb_context *ldb, const char *str)
112 {
113         size_t len = strlen(str);
114         void *ret;
115         ret = ldb_realloc(ldb, NULL, len+1);
116         if (ret) {
117                 memcpy(ret, str, len+1);
118         }
119         return ret;
120 }
121
122 /*
123   a ldb wrapper for asprintf(), using ldb_malloc()
124 */
125 int ldb_asprintf(struct ldb_context *ldb, char **strp, const char *fmt, ...) _PRINTF_ATTRIBUTE(3, 4)
126 {
127         int len, len2;
128         va_list ap;
129         
130         *strp = NULL;
131
132         va_start(ap, fmt);
133         len = vsnprintf(NULL, 0, fmt, ap);
134         va_end(ap);
135         if (len < 0) {
136                 return len;
137         }
138
139         *strp = ldb_malloc(ldb, len+1);
140         if (! *strp) {
141                 return -1;
142         }
143
144         va_start(ap, fmt);
145         len2 = vsnprintf(*strp, len+1, fmt, ap);
146         va_end(ap);
147
148         if (len2 != len) {
149                 /* buggy (or non-C99) vsnprintf function */
150                 ldb_free(ldb, *strp);
151                 return -1;
152         }
153
154         return len;
155 }
156
157 /*
158   realloc an array, checking for integer overflow in the array size
159 */
160 void *ldb_realloc_array(struct ldb_context *ldb,
161                         void *ptr, size_t el_size, unsigned count)
162 {
163 #define MAX_MALLOC_SIZE 0x7fffffff
164
165         if (count == 0 ||
166             count >= MAX_MALLOC_SIZE/el_size) {
167                 return NULL;
168         }
169         if (!ptr) {
170                 return ldb_malloc(ldb, el_size * count);
171         }
172         return ldb_realloc(ldb, ptr, el_size * count);
173 }
174