]> git.sur5r.net Git - openldap/blob - libraries/liblber/memory.c
97e3dff57b61f9fb24d2bdf8f9004d57398e12d5
[openldap] / libraries / liblber / memory.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 #include "portable.h"
7
8 #include <ac/stdlib.h>
9 #include <ac/string.h>
10
11 #include "lber-int.h"
12
13 #ifdef LDAP_MEMORY_TRACE
14 # ifndef LDAP_MEMORY_DEBUG
15 #  define LDAP_MEMORY_DEBUG 1
16 # endif
17 #include <stdio.h>
18 #endif
19
20 #if LDAP_MEMORY_DEBUG
21 /*
22  * LDAP_MEMORY_DEBUG should only be enabled for the purposes of
23  * debugging memory management within OpenLDAP libraries and slapd.
24  * It should only be enabled by an experienced developer as it
25  * causes the inclusion of numerous assert()'s, many of which may
26  * be triggered by a prefectly valid program.
27  *
28  * The code behind this macro is subject to change as needed to
29  * support this testing.
30  */
31
32 struct ber_mem_hdr {
33         ber_int_t       bm_top; /* Pattern to detect buf overrun from prev buffer */
34         ber_int_t       bm_length; /* Length of user allocated area */
35 #ifdef LDAP_MEMORY_TRACE
36         ber_int_t       bm_sequence; /* Allocation sequence number */
37 #endif
38         union bmu_align_u {     /* Force alignment, pattern to detect back clobber */
39                 ber_len_t       bmu_len_t;
40                 ber_tag_t       bmu_tag_t;
41                 ber_int_t       bmu_int_t;
42
43                 size_t  bmu_size_t;
44                 void *  bmu_voidp;
45                 double  bmu_double;
46                 long    bmu_long;
47                 long    (*bmu_funcp)( double );
48                 unsigned char   bmu_char[4];
49         } ber_align;
50 #define bm_junk ber_align.bmu_len_t
51 #define bm_data ber_align.bmu_char[1]
52 #define bm_char ber_align.bmu_char
53 };
54
55 /* Pattern at top of allocated space */
56 #define LBER_MEM_JUNK 0xdeaddadaU
57
58 static const struct ber_mem_hdr ber_int_mem_hdr = { LBER_MEM_JUNK, 0, 0 };
59
60 /* Note sequence and ber_int_options.lbu_meminuse are counters, but are not
61  * thread safe.  If you want to use these values for multithreaded applications,
62  * you must put mutexes around them, otherwise they will have incorrect values.
63  * When debugging, if you sort the debug output, the sequence number will 
64  * put allocations/frees together.  It is then a simple matter to write a script
65  * to find any allocations that don't have a buffer free function.
66  */
67 #ifdef LDAP_MEMORY_TRACE
68 static ber_int_t sequence = 0;
69 #endif
70
71 /* Pattern placed just before user data */
72 static unsigned char toppattern[4] = { 0xde, 0xad, 0xba, 0xde };
73 /* Pattern placed just after user data */
74 static unsigned char endpattern[4] = { 0xd1, 0xed, 0xde, 0xca };
75
76 #define mbu_len sizeof(ber_int_mem_hdr.ber_align)
77
78 /* Test if pattern placed just before user data is good */
79 #define testdatatop(val) ( \
80         *(val->bm_char+mbu_len-4)==toppattern[0] && \
81         *(val->bm_char+mbu_len-3)==toppattern[1] && \
82         *(val->bm_char+mbu_len-2)==toppattern[2] && \
83         *(val->bm_char+mbu_len-1)==toppattern[3] )
84
85 /* Place pattern just before user data */
86 #define setdatatop(val) *(val->bm_char+mbu_len-4)=toppattern[0]; \
87         *(val->bm_char+mbu_len-3)=toppattern[1]; \
88         *(val->bm_char+mbu_len-2)=toppattern[2]; \
89         *(val->bm_char+mbu_len-1)=toppattern[3];
90
91 /* Test if pattern placed just after user data is good */
92 #define testend(val) (  *((unsigned char *)val+0)==endpattern[0] && \
93         *((unsigned char *)val+1)==endpattern[1] && \
94         *((unsigned char *)val+2)==endpattern[2] && \
95         *((unsigned char *)val+3)==endpattern[3] )
96
97 /* Place pattern just after user data */
98 #define setend(val)     *((unsigned char *)val+0)=endpattern[0]; \
99         *((unsigned char *)val+1)=endpattern[1]; \
100         *((unsigned char *)val+2)=endpattern[2]; \
101         *((unsigned char *)val+3)=endpattern[3];
102
103 #define BER_MEM_BADADDR ((void *) &ber_int_mem_hdr.bm_data)
104 #define BER_MEM_VALID(p)        do { \
105                 assert( (p) != BER_MEM_BADADDR );       \
106                 assert( (p) != (void *) &ber_int_mem_hdr );     \
107         } while(0)
108
109 #else
110 #define BER_MEM_VALID(p)        /* no-op */
111 #endif
112
113 BerMemoryFunctions *ber_int_memory_fns = NULL;
114
115 void
116 ber_memfree_x( void *p, void *ctx )
117 {
118     ber_int_options.lbo_valid = LBER_INITIALIZED;
119
120         if( p == NULL ) {
121                 return;
122         }
123
124         BER_MEM_VALID( p );
125
126         if( ber_int_memory_fns == NULL || ctx == NULL ) {
127 #ifdef LDAP_MEMORY_DEBUG
128                 struct ber_mem_hdr *mh = (struct ber_mem_hdr *)
129                         ((char *)p - sizeof(struct ber_mem_hdr));
130                 assert( mh->bm_top == LBER_MEM_JUNK);
131                 assert( testdatatop( mh));
132                 assert( testend( (char *)&mh[1] + mh->bm_length) );
133                 ber_int_options.lbo_meminuse -= mh->bm_length;
134
135 #ifdef LDAP_MEMORY_TRACE
136                 fprintf(stderr, "0x%08x 0x%08x -f- %d ber_memfree %d\n",
137                         mh->bm_sequence, mh, mh->bm_length, ber_int_options.lbo_meminuse);
138 #endif
139                 /* Fill the free space with poison */
140                 memset( mh, 0xff, mh->bm_length + sizeof(struct ber_mem_hdr) + sizeof(ber_int_t));
141                 free( mh );
142 #else
143                 free( p );
144 #endif
145                 return;
146         }
147
148         assert( ber_int_memory_fns->bmf_free );
149
150         (*ber_int_memory_fns->bmf_free)( p, ctx );
151 }
152
153 void
154 ber_memfree( void *p )
155 {
156         ber_memfree_x(p, NULL);
157 }
158
159 void
160 ber_memvfree_x( void **vec, void *ctx )
161 {
162         int     i;
163
164         ber_int_options.lbo_valid = LBER_INITIALIZED;
165
166         if( vec == NULL ) {
167                 return;
168         }
169
170         BER_MEM_VALID( vec );
171
172         for ( i = 0; vec[i] != NULL; i++ ) {
173                 ber_memfree_x( vec[i], ctx );
174         }
175
176         ber_memfree_x( vec, ctx );
177 }
178
179 void
180 ber_memvfree( void **vec )
181 {
182         ber_memvfree_x( vec, NULL );
183 }
184
185 void *
186 ber_memalloc_x( ber_len_t s, void *ctx )
187 {
188         void *new;
189         ber_int_options.lbo_valid = LBER_INITIALIZED;
190
191 #ifdef LDAP_MEMORY_DEBUG
192         assert( s != 0 );
193 #endif
194
195         if( s == 0 ) {
196                 return NULL;
197         }
198
199         if( ber_int_memory_fns == NULL || ctx == NULL ) {
200 #ifdef LDAP_MEMORY_DEBUG
201                 struct ber_mem_hdr *mh = malloc(s + sizeof(struct ber_mem_hdr) + sizeof( ber_int_t));
202                 if( mh == NULL ) return NULL;
203
204                 mh->bm_top = LBER_MEM_JUNK;
205                 mh->bm_length = s;
206                 setdatatop( mh);
207                 setend( (char *)&mh[1] + mh->bm_length );
208
209                 ber_int_options.lbo_meminuse += mh->bm_length;  /* Count mem inuse */
210
211 #ifdef LDAP_MEMORY_TRACE
212                 mh->bm_sequence = sequence++;
213                 fprintf(stderr, "0x%08x 0x%08x -a- %d ber_memalloc %d\n",
214                         mh->bm_sequence, mh, mh->bm_length, ber_int_options.lbo_meminuse);
215 #endif
216                 /* poison new memory */
217                 memset( (char *)&mh[1], 0xff, s);
218
219                 BER_MEM_VALID( &mh[1] );
220                 new = &mh[1];
221 #else
222                 new = malloc( s );
223 #endif
224         } else {
225                 new = (*ber_int_memory_fns->bmf_malloc)( s, ctx );
226         }
227
228         if( new == NULL ) {
229                 ber_errno = LBER_ERROR_MEMORY;
230         }
231
232         return new;
233 }
234
235 void *
236 ber_memalloc( ber_len_t s )
237 {
238         return ber_memalloc_x( s, NULL );
239 }
240
241 void *
242 ber_memcalloc_x( ber_len_t n, ber_len_t s, void *ctx )
243 {
244         void *new;
245         ber_int_options.lbo_valid = LBER_INITIALIZED;
246
247 #ifdef LDAP_MEMORY_DEBUG
248         assert( n != 0 && s != 0);
249 #endif
250
251         if( n == 0 || s == 0 ) {
252                 return NULL;
253         }
254
255         if( ber_int_memory_fns == NULL || ctx == NULL ) {
256 #ifdef LDAP_MEMORY_DEBUG
257                 struct ber_mem_hdr *mh = calloc(1,
258                         (n * s) + sizeof(struct ber_mem_hdr) + sizeof(ber_int_t) );
259                 if( mh == NULL ) return NULL;
260
261                 mh->bm_top = LBER_MEM_JUNK;
262                 mh->bm_length = n*s;
263                 setdatatop( mh);
264                 setend( (char *)&mh[1] + mh->bm_length );
265
266                 ber_int_options.lbo_meminuse += mh->bm_length;
267
268 #ifdef LDAP_MEMORY_TRACE
269                 mh->bm_sequence = sequence++;
270                 fprintf(stderr, "0x%08x 0x%08x -a- %d ber_memcalloc %d\n",
271                         mh->bm_sequence, mh, mh->bm_length, ber_int_options.lbo_meminuse);
272 #endif
273                 BER_MEM_VALID( &mh[1] );
274                 new = &mh[1];
275 #else
276                 new = calloc( n, s );
277 #endif
278
279         } else {
280                 new = (*ber_int_memory_fns->bmf_calloc)( n, s, ctx );
281         }
282
283         if( new == NULL ) {
284                 ber_errno = LBER_ERROR_MEMORY;
285         }
286
287         return new;
288 }
289
290 void *
291 ber_memcalloc( ber_len_t n, ber_len_t s )
292 {
293         return ber_memcalloc_x( n, s, NULL );
294 }
295
296 void *
297 ber_memrealloc_x( void* p, ber_len_t s, void *ctx )
298 {
299         void *new = NULL;
300         ber_int_options.lbo_valid = LBER_INITIALIZED;
301
302         /* realloc(NULL,s) -> malloc(s) */
303         if( p == NULL ) {
304                 return ber_memalloc_x( s, ctx );
305         }
306         
307         /* realloc(p,0) -> free(p) */
308         if( s == 0 ) {
309                 ber_memfree_x( p, ctx );
310                 return NULL;
311         }
312
313         BER_MEM_VALID( p );
314
315         if( ber_int_memory_fns == NULL || ctx == NULL ) {
316 #ifdef LDAP_MEMORY_DEBUG
317                 ber_int_t oldlen;
318                 struct ber_mem_hdr *mh = (struct ber_mem_hdr *)
319                         ((char *)p - sizeof(struct ber_mem_hdr));
320                 assert( mh->bm_top == LBER_MEM_JUNK);
321                 assert( testdatatop( mh));
322                 assert( testend( (char *)&mh[1] + mh->bm_length) );
323                 oldlen = mh->bm_length;
324
325                 p = realloc( mh, s + sizeof(struct ber_mem_hdr) + sizeof(ber_int_t) );
326                 if( p == NULL ) {
327                         ber_errno = LBER_ERROR_MEMORY;
328                         return NULL;
329                 }
330
331                         mh = p;
332                 mh->bm_length = s;
333                 setend( (char *)&mh[1] + mh->bm_length );
334                 if( (s - oldlen) > 0 ) {
335                         /* poison any new memory */
336                         memset( (char *)&mh[1] + oldlen, 0xff, s - oldlen);
337                 }
338
339                 assert( mh->bm_top == LBER_MEM_JUNK);
340                 assert( testdatatop( mh));
341
342                 ber_int_options.lbo_meminuse += s - oldlen;
343 #ifdef LDAP_MEMORY_TRACE
344                 fprintf(stderr, "0x%08x 0x%08x -a- %d ber_memrealloc %d\n",
345                         mh->bm_sequence, mh, mh->bm_length, ber_int_options.lbo_meminuse);
346 #endif
347                         BER_MEM_VALID( &mh[1] );
348                 return &mh[1];
349 #else
350                 new = realloc( p, s );
351 #endif
352         } else {
353                 new = (*ber_int_memory_fns->bmf_realloc)( p, s, ctx );
354         }
355
356         if( new == NULL ) {
357                 ber_errno = LBER_ERROR_MEMORY;
358         }
359
360         return new;
361 }
362
363 void *
364 ber_memrealloc( void* p, ber_len_t s )
365 {
366         return ber_memrealloc_x( p, s, NULL );
367 }
368
369 void
370 ber_bvfree_x( struct berval *bv, void *ctx )
371 {
372         ber_int_options.lbo_valid = LBER_INITIALIZED;
373
374         if( bv == NULL ) {
375                 return;
376         }
377
378         BER_MEM_VALID( bv );
379
380         if ( bv->bv_val != NULL ) {
381                 ber_memfree_x( bv->bv_val, ctx );
382         }
383
384         ber_memfree_x( (char *) bv, ctx );
385 }
386
387 void
388 ber_bvfree( struct berval *bv )
389 {
390         ber_bvfree_x( bv, NULL );
391 }
392
393 void
394 ber_bvecfree_x( struct berval **bv, void *ctx )
395 {
396         int     i;
397
398         ber_int_options.lbo_valid = LBER_INITIALIZED;
399
400         if( bv == NULL ) {
401                 return;
402         }
403
404         BER_MEM_VALID( bv );
405
406         /* count elements */
407         for ( i = 0; bv[i] != NULL; i++ ) ;
408
409         /* free in reverse order */
410         for ( i--; i >= 0; i-- ) {
411                 ber_bvfree_x( bv[i], ctx );
412         }
413
414         ber_memfree_x( (char *) bv, ctx );
415 }
416
417 void
418 ber_bvecfree( struct berval **bv )
419 {
420         ber_bvecfree_x( bv, NULL );
421 }
422
423 int
424 ber_bvecadd_x( struct berval ***bvec, struct berval *bv, void *ctx )
425 {
426         ber_len_t i;
427         struct berval **new;
428
429         ber_int_options.lbo_valid = LBER_INITIALIZED;
430
431         if( *bvec == NULL ) {
432                 if( bv == NULL ) {
433                         /* nothing to add */
434                         return 0;
435                 }
436
437                 *bvec = ber_memalloc_x( 2 * sizeof(struct berval *), ctx );
438
439                 if( *bvec == NULL ) {
440                         return -1;
441                 }
442
443                 (*bvec)[0] = bv;
444                 (*bvec)[1] = NULL;
445
446                 return 1;
447         }
448
449         BER_MEM_VALID( bvec );
450
451         /* count entries */
452         for ( i = 0; (*bvec)[i] != NULL; i++ ) {
453                 /* EMPTY */;
454         }
455
456         if( bv == NULL ) {
457                 return i;
458         }
459
460         new = ber_memrealloc_x( *bvec, (i+2) * sizeof(struct berval *), ctx);
461
462         if( new == NULL ) {
463                 return -1;
464         }
465
466         *bvec = new;
467
468         (*bvec)[i++] = bv;
469         (*bvec)[i] = NULL;
470
471         return i;
472 }
473
474 int
475 ber_bvecadd( struct berval ***bvec, struct berval *bv )
476 {
477         return ber_bvecadd_x( bvec, bv, NULL );
478 }
479
480 struct berval *
481 ber_dupbv_x(
482         struct berval *dst, struct berval *src, void *ctx )
483 {
484         struct berval *new;
485
486         ber_int_options.lbo_valid = LBER_INITIALIZED;
487
488         if( src == NULL ) {
489                 ber_errno = LBER_ERROR_PARAM;
490                 return NULL;
491         }
492
493         if ( dst ) {
494                 new = dst;
495         } else {
496                 if(( new = ber_memalloc_x( sizeof(struct berval), ctx )) == NULL ) {
497                         ber_errno = LBER_ERROR_MEMORY;
498                         return NULL;
499                 }
500         }
501
502         if ( src->bv_val == NULL ) {
503                 new->bv_val = NULL;
504                 new->bv_len = 0;
505                 return new;
506         }
507
508         if(( new->bv_val = ber_memalloc_x( src->bv_len + 1, ctx )) == NULL ) {
509                 ber_errno = LBER_ERROR_MEMORY;
510                 if ( !dst )
511                         ber_memfree_x( new, ctx );
512                 return NULL;
513         }
514
515         AC_MEMCPY( new->bv_val, src->bv_val, src->bv_len );
516         new->bv_val[src->bv_len] = '\0';
517         new->bv_len = src->bv_len;
518
519         return new;
520 }
521
522 struct berval *
523 ber_dupbv(
524         struct berval *dst, struct berval *src )
525 {
526         return ber_dupbv_x( dst, src, NULL );
527 }
528
529 struct berval *
530 ber_bvdup(
531         struct berval *src )
532 {
533         return ber_dupbv_x( NULL, src, NULL );
534 }
535
536 struct berval *
537 ber_str2bv_x(
538         LDAP_CONST char *s, ber_len_t len, int dup, struct berval *bv,
539         void *ctx)
540 {
541         struct berval *new;
542
543         ber_int_options.lbo_valid = LBER_INITIALIZED;
544
545         if( s == NULL ) {
546                 ber_errno = LBER_ERROR_PARAM;
547                 return NULL;
548         }
549
550         if( bv ) {
551                 new = bv;
552         } else {
553                 if(( new = ber_memalloc_x( sizeof(struct berval), ctx )) == NULL ) {
554                         ber_errno = LBER_ERROR_MEMORY;
555                         return NULL;
556                 }
557         }
558
559         new->bv_len = len ? len : strlen( s );
560         if ( dup ) {
561                 if ( (new->bv_val = ber_memalloc_x( new->bv_len+1, ctx )) == NULL ) {
562                         ber_errno = LBER_ERROR_MEMORY;
563                         if ( !bv )
564                                 ber_memfree_x( new, ctx );
565                         return NULL;
566                 }
567
568                 AC_MEMCPY( new->bv_val, s, new->bv_len );
569                 new->bv_val[new->bv_len] = '\0';
570         } else {
571                 new->bv_val = (char *) s;
572         }
573
574         return( new );
575 }
576
577 struct berval *
578 ber_str2bv(
579         LDAP_CONST char *s, ber_len_t len, int dup, struct berval *bv)
580 {
581         return ber_str2bv_x( s, len, dup, bv, NULL );
582 }
583
584 struct berval *
585 ber_mem2bv_x(
586         LDAP_CONST char *s, ber_len_t len, int dup, struct berval *bv,
587         void *ctx)
588 {
589         struct berval *new;
590
591         ber_int_options.lbo_valid = LBER_INITIALIZED;
592
593         if( s == NULL ) {
594                 ber_errno = LBER_ERROR_PARAM;
595                 return NULL;
596         }
597
598         if( bv ) {
599                 new = bv;
600         } else {
601                 if(( new = ber_memalloc_x( sizeof(struct berval), ctx )) == NULL ) {
602                         ber_errno = LBER_ERROR_MEMORY;
603                         return NULL;
604                 }
605         }
606
607         new->bv_len = len;
608         if ( dup ) {
609                 if ( (new->bv_val = ber_memalloc_x( new->bv_len+1, ctx )) == NULL ) {
610                         ber_errno = LBER_ERROR_MEMORY;
611                         if ( !bv ) {
612                                 ber_memfree_x( new, ctx );
613                         }
614                         return NULL;
615                 }
616
617                 AC_MEMCPY( new->bv_val, s, new->bv_len );
618                 new->bv_val[new->bv_len] = '\0';
619         } else {
620                 new->bv_val = (char *) s;
621         }
622
623         return( new );
624 }
625
626 struct berval *
627 ber_mem2bv(
628         LDAP_CONST char *s, ber_len_t len, int dup, struct berval *bv)
629 {
630         return ber_mem2bv_x( s, len, dup, bv, NULL );
631 }
632
633 char *
634 ber_strdup_x( LDAP_CONST char *s, void *ctx )
635 {
636         char    *p;
637         size_t  len;
638         
639         ber_int_options.lbo_valid = LBER_INITIALIZED;
640
641 #ifdef LDAP_MEMORY_DEBUG
642         assert(s != NULL);                      /* bv damn better point to something */
643 #endif
644
645         if( s == NULL ) {
646                 ber_errno = LBER_ERROR_PARAM;
647                 return NULL;
648         }
649
650         len = strlen( s ) + 1;
651
652         if ( (p = ber_memalloc_x( len, ctx )) == NULL ) {
653                 ber_errno = LBER_ERROR_MEMORY;
654                 return NULL;
655         }
656
657         AC_MEMCPY( p, s, len );
658         return p;
659 }
660
661 char *
662 ber_strdup( LDAP_CONST char *s )
663 {
664         return ber_strdup_x( s, NULL );
665 }
666
667 char *
668 ber_strndup_x( LDAP_CONST char *s, ber_len_t l, void *ctx )
669 {
670         char    *p;
671         size_t  len;
672         
673         ber_int_options.lbo_valid = LBER_INITIALIZED;
674
675 #ifdef LDAP_MEMORY_DEBUG
676         assert(s != NULL);                      /* bv damn better point to something */
677 #endif
678
679         if( s == NULL ) {
680                 ber_errno = LBER_ERROR_PARAM;
681                 return NULL;
682         }
683
684         len = strlen( s );
685
686         if ( len > l ) {
687                 len = l;
688         }
689
690         if ( (p = ber_memalloc_x( len + 1, ctx )) == NULL ) {
691                 ber_errno = LBER_ERROR_MEMORY;
692                 return NULL;
693         }
694
695         AC_MEMCPY( p, s, len );
696         p[len] = '\0';
697         return p;
698 }
699
700 char *
701 ber_strndup( LDAP_CONST char *s, ber_len_t l )
702 {
703         return ber_strndup_x( s, l, NULL );
704 }
705
706 void
707 ber_bvarray_free_x( BerVarray a, void *ctx )
708 {
709         int i;
710
711         ber_int_options.lbo_valid = LBER_INITIALIZED;
712
713         if (a) {
714                 BER_MEM_VALID( a );
715
716                 /* count elements */
717                 for (i=0; a[i].bv_val; i++) ;
718                 
719                 /* free in reverse order */
720                 for (i--; i>=0; i--) {
721                         ber_memfree_x(a[i].bv_val, ctx);
722                 }
723
724                 ber_memfree_x(a, ctx);
725         }
726 }
727
728 void
729 ber_bvarray_free( BerVarray a )
730 {
731         ber_bvarray_free_x(a, NULL);
732 }
733
734 int
735 ber_bvarray_add_x( BerVarray *a, BerValue *bv, void *ctx )
736 {
737         int     n;
738
739         ber_int_options.lbo_valid = LBER_INITIALIZED;
740
741         if ( *a == NULL ) {
742                 if (bv == NULL) {
743                         return 0;
744                 }
745                 n = 0;
746
747                 *a = (BerValue *) ber_memalloc_x( 2 * sizeof(BerValue), ctx );
748                 if ( *a == NULL ) {
749                         return -1;
750                 }
751
752         } else {
753                 BerVarray atmp;
754                 BER_MEM_VALID( a );
755
756                 for ( n = 0; *a != NULL && (*a)[n].bv_val != NULL; n++ ) {
757                         ;       /* just count them */
758                 }
759
760                 if (bv == NULL) {
761                         return n;
762                 }
763
764                 atmp = (BerValue *) ber_memrealloc_x( (char *) *a,
765                     (n + 2) * sizeof(BerValue), ctx );
766
767                 if( atmp == NULL ) {
768                         return -1;
769                 }
770
771                 *a = atmp;
772         }
773
774         (*a)[n++] = *bv;
775         (*a)[n].bv_val = NULL;
776
777         return n;
778 }
779
780 int
781 ber_bvarray_add( BerVarray *a, BerValue *bv )
782 {
783         return ber_bvarray_add_x( a, bv, NULL );
784 }