1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2006 Ricardo Correia. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #include <umem.h> 27 #include <stdlib.h> 28 #include <assert.h> 29 30 static umem_nofail_callback_t *nofail_cb = NULL; 31 32 struct umem_cache { 33 umem_constructor_t *constructor; 34 umem_destructor_t *destructor; 35 void *callback_data; 36 size_t bufsize; 37 }; 38 39 /* 40 * Simple stub for umem_alloc(). The callback isn't expected to return. 41 */ 42 void *umem_alloc(size_t size, int flags) 43 { 44 assert(flags == UMEM_DEFAULT || flags == UMEM_NOFAIL); 45 46 if(size == 0) 47 return NULL; 48 49 void *ret = malloc(size); 50 if(ret == NULL) { 51 if(!(flags & UMEM_NOFAIL)) 52 return NULL; 53 54 if(nofail_cb != NULL) 55 nofail_cb(); 56 abort(); 57 } 58 59 return ret; 60 } 61 62 /* 63 * Simple stub for umem_zalloc(). 64 */ 65 void *umem_zalloc(size_t size, int flags) 66 { 67 assert(flags == UMEM_DEFAULT || flags == UMEM_NOFAIL); 68 69 if(size == 0) 70 return NULL; 71 72 void *ret = calloc(1, size); 73 if(ret == NULL) { 74 if(!(flags & UMEM_NOFAIL)) 75 return NULL; 76 77 if(nofail_cb != NULL) 78 nofail_cb(); 79 abort(); 80 } 81 82 return ret; 83 } 84 85 /* 86 * Simple stub for umem_free(). 87 */ 88 void umem_free(void *buf, size_t size) 89 { 90 free(buf); 91 } 92 93 /* 94 * Simple stub for umem_nofail_callback(). 95 */ 96 void umem_nofail_callback(umem_nofail_callback_t *callback) 97 { 98 nofail_cb = callback; 99 } 100 101 /* 102 * Simple stub for umem_cache_create(). 103 */ 104 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) 105 { 106 assert(source == NULL); 107 108 umem_cache_t *cache = malloc(sizeof(umem_cache_t)); 109 if(cache == NULL) 110 return NULL; 111 112 cache->constructor = constructor; 113 cache->destructor = destructor; 114 cache->callback_data = callback_data; 115 cache->bufsize = bufsize; 116 117 return cache; 118 } 119 120 /* 121 * Simple stub for umem_cache_alloc(). The nofail callback isn't expected to return. 122 */ 123 void *umem_cache_alloc(umem_cache_t *cache, int flags) 124 { 125 void *buf = malloc(cache->bufsize); 126 if(buf == NULL) { 127 if(!(flags & UMEM_NOFAIL)) 128 return NULL; 129 130 if(nofail_cb != NULL) 131 nofail_cb(); 132 abort(); 133 } 134 135 if(cache->constructor != NULL) { 136 if(cache->constructor(buf, cache->callback_data, flags) != 0) { 137 free(buf); 138 if(!(flags & UMEM_NOFAIL)) 139 return NULL; 140 141 if(nofail_cb != NULL) 142 nofail_cb(); 143 abort(); 144 } 145 } 146 147 return buf; 148 } 149 150 /* 151 * Simple stub for umem_cache_free(). 152 */ 153 void umem_cache_free(umem_cache_t *cache, void *buffer) 154 { 155 if(cache->destructor != NULL) 156 cache->destructor(buffer, cache->callback_data); 157 158 free(buffer); 159 } 160 161 /* 162 * Simple stub for umem_cache_destroy(). 163 */ 164 void umem_cache_destroy(umem_cache_t *cache) 165 { 166 free(cache); 167 } 168