1 /* $NetBSD: umem.c,v 1.1 2009/03/26 22:11:44 ad Exp $ */
6 * The contents of this file are subject to the terms of the
7 * Common Development and Distribution License (the "License").
8 * You may not use this file except in compliance with the License.
10 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11 * or http://www.opensolaris.org/os/licensing.
12 * See the License for the specific language governing permissions
13 * and limitations under the License.
15 * When distributing Covered Code, include this CDDL HEADER in each
16 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17 * If applicable, add the following below this CDDL HEADER, with the
18 * fields enclosed by brackets "[]" replaced with your own identifying
19 * information: Portions Copyright [yyyy] [name of copyright owner]
24 * Copyright 2006 Ricardo Correia. All rights reserved.
25 * Use is subject to license terms.
32 static umem_nofail_callback_t
*nofail_cb
= NULL
;
35 umem_constructor_t
*constructor
;
36 umem_destructor_t
*destructor
;
42 * Simple stub for umem_alloc(). The callback isn't expected to return.
44 void *umem_alloc(size_t size
, int flags
)
46 assert(flags
== UMEM_DEFAULT
|| flags
== UMEM_NOFAIL
);
51 void *ret
= malloc(size
);
53 if(!(flags
& UMEM_NOFAIL
))
65 * Simple stub for umem_zalloc().
67 void *umem_zalloc(size_t size
, int flags
)
69 assert(flags
== UMEM_DEFAULT
|| flags
== UMEM_NOFAIL
);
74 void *ret
= calloc(1, size
);
76 if(!(flags
& UMEM_NOFAIL
))
88 * Simple stub for umem_free().
90 void umem_free(void *buf
, size_t size
)
96 * Simple stub for umem_nofail_callback().
98 void umem_nofail_callback(umem_nofail_callback_t
*callback
)
100 nofail_cb
= callback
;
104 * Simple stub for umem_cache_create().
106 umem_cache_t
*umem_cache_create(char *debug_name
, size_t bufsize
, size_t align
, umem_constructor_t
*constructor
, umem_destructor_t
*destructor
, umem_reclaim_t
*reclaim
, void *callback_data
, void *source
, int cflags
)
108 assert(source
== NULL
);
110 umem_cache_t
*cache
= malloc(sizeof(umem_cache_t
));
114 cache
->constructor
= constructor
;
115 cache
->destructor
= destructor
;
116 cache
->callback_data
= callback_data
;
117 cache
->bufsize
= bufsize
;
123 * Simple stub for umem_cache_alloc(). The nofail callback isn't expected to return.
125 void *umem_cache_alloc(umem_cache_t
*cache
, int flags
)
127 void *buf
= malloc(cache
->bufsize
);
129 if(!(flags
& UMEM_NOFAIL
))
132 if(nofail_cb
!= NULL
)
137 if(cache
->constructor
!= NULL
) {
138 if(cache
->constructor(buf
, cache
->callback_data
, flags
) != 0) {
140 if(!(flags
& UMEM_NOFAIL
))
143 if(nofail_cb
!= NULL
)
153 * Simple stub for umem_cache_free().
155 void umem_cache_free(umem_cache_t
*cache
, void *buffer
)
157 if(cache
->destructor
!= NULL
)
158 cache
->destructor(buffer
, cache
->callback_data
);
164 * Simple stub for umem_cache_destroy().
166 void umem_cache_destroy(umem_cache_t
*cache
)