xref: /netbsd-src/external/bsd/openldap/dist/libraries/libldap/free.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: free.c,v 1.1.1.2 2010/03/08 02:14:20 lukem Exp $	*/
2 
3 /* free.c */
4 /* OpenLDAP: pkg/ldap/libraries/libldap/free.c,v 1.22.2.4 2009/01/22 00:00:54 kurt Exp */
5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6  *
7  * Copyright 1998-2009 The OpenLDAP Foundation.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted only as authorized by the OpenLDAP
12  * Public License.
13  *
14  * A copy of this license is available in the file LICENSE in the
15  * top-level directory of the distribution or, alternatively, at
16  * <http://www.OpenLDAP.org/license.html>.
17  */
18 /* Portions Copyright (c) 1994 The Regents of the University of Michigan.
19  * All rights reserved.
20  */
21 
22 /*
23  *  free.c - some free routines are included here to avoid having to
24  *           link in lots of extra code when not using certain features
25  */
26 
27 #include "portable.h"
28 
29 #include <stdio.h>
30 #include <ac/stdlib.h>
31 
32 #include <ac/string.h>
33 #include <ac/time.h>
34 
35 #include "ldap-int.h"
36 
37 /*
38  * C-API deallocator
39  */
40 void
41 ldap_memfree( void *p )
42 {
43 	LDAP_FREE( p );
44 }
45 
46 void
47 ldap_memvfree( void **v )
48 {
49 	LDAP_VFREE( v );
50 }
51 
52 void *
53 ldap_memalloc( ber_len_t s )
54 {
55 	return LDAP_MALLOC( s );
56 }
57 
58 void *
59 ldap_memcalloc( ber_len_t n, ber_len_t s )
60 {
61 	return LDAP_CALLOC( n, s );
62 }
63 
64 void *
65 ldap_memrealloc( void* p, ber_len_t s )
66 {
67 	return LDAP_REALLOC( p, s );
68 }
69 
70 char *
71 ldap_strdup( LDAP_CONST char *p )
72 {
73 	return LDAP_STRDUP( p );
74 }
75 
76 /*
77  * free a null-terminated array of pointers to mod structures. the
78  * structures are freed, not the array itself, unless the freemods
79  * flag is set.
80  */
81 
82 void
83 ldap_mods_free( LDAPMod **mods, int freemods )
84 {
85 	int	i;
86 
87 	if ( mods == NULL )
88 		return;
89 
90 	for ( i = 0; mods[i] != NULL; i++ ) {
91 		if ( mods[i]->mod_op & LDAP_MOD_BVALUES ) {
92 			if( mods[i]->mod_bvalues != NULL )
93 				ber_bvecfree( mods[i]->mod_bvalues );
94 
95 		} else if( mods[i]->mod_values != NULL ) {
96 			LDAP_VFREE( mods[i]->mod_values );
97 		}
98 
99 		if ( mods[i]->mod_type != NULL ) {
100 			LDAP_FREE( mods[i]->mod_type );
101 		}
102 
103 		LDAP_FREE( (char *) mods[i] );
104 	}
105 
106 	if ( freemods ) {
107 		LDAP_FREE( (char *) mods );
108 	}
109 }
110