Path: blob/main/cddl/compat/opensolaris/lib/libumem/umem.c
39488 views
/*1* CDDL HEADER START2*3* The contents of this file are subject to the terms of the4* Common Development and Distribution License (the "License").5* You may not use this file except in compliance with the License.6*7* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE8* or http://www.opensolaris.org/os/licensing.9* See the License for the specific language governing permissions10* and limitations under the License.11*12* When distributing Covered Code, include this CDDL HEADER in each13* file and include the License file at usr/src/OPENSOLARIS.LICENSE.14* If applicable, add the following below this CDDL HEADER, with the15* fields enclosed by brackets "[]" replaced with your own identifying16* information: Portions Copyright [yyyy] [name of copyright owner]17*18* CDDL HEADER END19*/20/*21* Copyright 2006 Ricardo Correia. All rights reserved.22* Use is subject to license terms.23*/2425#include <umem.h>26#include <stdlib.h>27#include <assert.h>2829static umem_nofail_callback_t *nofail_cb = NULL;3031struct umem_cache {32umem_constructor_t *constructor;33umem_destructor_t *destructor;34void *callback_data;35size_t bufsize;36};3738/*39* Simple stub for umem_alloc(). The callback isn't expected to return.40*/41void *umem_alloc(size_t size, int flags)42{43assert(flags == UMEM_DEFAULT || flags == UMEM_NOFAIL);4445if(size == 0)46return NULL;4748void *ret = malloc(size);49if(ret == NULL) {50if(!(flags & UMEM_NOFAIL))51return NULL;5253if(nofail_cb != NULL)54nofail_cb();55abort();56}5758return ret;59}6061/*62* Simple stub for umem_zalloc().63*/64void *umem_zalloc(size_t size, int flags)65{66assert(flags == UMEM_DEFAULT || flags == UMEM_NOFAIL);6768if(size == 0)69return NULL;7071void *ret = calloc(1, size);72if(ret == NULL) {73if(!(flags & UMEM_NOFAIL))74return NULL;7576if(nofail_cb != NULL)77nofail_cb();78abort();79}8081return ret;82}8384/*85* Simple stub for umem_free().86*/87void umem_free(void *buf, size_t size)88{89free(buf);90}9192/*93* Simple stub for umem_nofail_callback().94*/95void umem_nofail_callback(umem_nofail_callback_t *callback)96{97nofail_cb = callback;98}99100/*101* Simple stub for umem_cache_create().102*/103umem_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)104{105assert(source == NULL);106107umem_cache_t *cache = malloc(sizeof(umem_cache_t));108if(cache == NULL)109return NULL;110111cache->constructor = constructor;112cache->destructor = destructor;113cache->callback_data = callback_data;114cache->bufsize = bufsize;115116return cache;117}118119/*120* Simple stub for umem_cache_alloc(). The nofail callback isn't expected to return.121*/122void *umem_cache_alloc(umem_cache_t *cache, int flags)123{124void *buf = malloc(cache->bufsize);125if(buf == NULL) {126if(!(flags & UMEM_NOFAIL))127return NULL;128129if(nofail_cb != NULL)130nofail_cb();131abort();132}133134if(cache->constructor != NULL) {135if(cache->constructor(buf, cache->callback_data, flags) != 0) {136free(buf);137if(!(flags & UMEM_NOFAIL))138return NULL;139140if(nofail_cb != NULL)141nofail_cb();142abort();143}144}145146return buf;147}148149/*150* Simple stub for umem_cache_free().151*/152void umem_cache_free(umem_cache_t *cache, void *buffer)153{154if(cache->destructor != NULL)155cache->destructor(buffer, cache->callback_data);156157free(buffer);158}159160/*161* Simple stub for umem_cache_destroy().162*/163void umem_cache_destroy(umem_cache_t *cache)164{165free(cache);166}167168169