1N/A/*-
1N/A * See the file LICENSE for redistribution information.
1N/A *
1N/A * Copyright (c) 1997, 1998
1N/A * Sleepycat Software. All rights reserved.
1N/A */
1N/A
1N/A#include "config.h"
1N/A
1N/A#ifndef lint
1N/Astatic const char sccsid[] = "@(#)os_alloc.c 10.10 (Sleepycat) 10/12/98";
1N/A#endif /* not lint */
1N/A
1N/A#ifndef NO_SYSTEM_INCLUDES
1N/A#include <sys/types.h>
1N/A
1N/A#include <errno.h>
1N/A#include <string.h>
1N/A#include <stdlib.h>
1N/A#endif
1N/A
1N/A#include "db_int.h"
1N/A#include "os_jump.h"
1N/A
1N/A/*
1N/A * !!!
1N/A * Correct for systems that return NULL when you allocate 0 bytes of memory.
1N/A * There are several places in DB where we allocate the number of bytes held
1N/A * by the key/data item, and it can be 0. Correct here so that malloc never
1N/A * returns a NULL for that reason (which behavior is permitted by ANSI). We
1N/A * could make these calls macros on non-Alpha architectures (that's where we
1N/A * saw the problem), but it's probably not worth the autoconf complexity.
1N/A *
1N/A * !!!
1N/A * Correct for systems that don't set errno when malloc and friends fail.
1N/A *
1N/A * Out of memory.
1N/A * We wish to hold the whole sky,
1N/A * But we never will.
1N/A */
1N/A
1N/A/*
1N/A * __os_strdup --
1N/A * The strdup(3) function for DB.
1N/A *
1N/A * PUBLIC: int __os_strdup __P((const char *, void *));
1N/A */
1N/Aint
1N/A__os_strdup(str, storep)
1N/A const char *str;
1N/A void *storep;
1N/A{
1N/A size_t size;
1N/A int ret;
1N/A void *p;
1N/A
1N/A *(void **)storep = NULL;
1N/A
1N/A size = strlen(str) + 1;
1N/A if ((ret = __os_malloc(size, NULL, &p)) != 0)
1N/A return (ret);
1N/A
1N/A memcpy(p, str, size);
1N/A
1N/A *(void **)storep = p;
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * __os_calloc --
1N/A * The calloc(3) function for DB.
1N/A *
1N/A * PUBLIC: int __os_calloc __P((size_t, size_t, void *));
1N/A */
1N/Aint
1N/A__os_calloc(num, size, storep)
1N/A size_t num, size;
1N/A void *storep;
1N/A{
1N/A void *p;
1N/A int ret;
1N/A
1N/A size *= num;
1N/A if ((ret = __os_malloc(size, NULL, &p)) != 0)
1N/A return (ret);
1N/A
1N/A memset(p, 0, size);
1N/A *(void **)storep = p;
1N/A
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * __os_malloc --
1N/A * The malloc(3) function for DB.
1N/A *
1N/A * PUBLIC: int __os_malloc __P((size_t, void *(*)(size_t), void *));
1N/A */
1N/Aint
1N/A__os_malloc(size, db_malloc, storep)
1N/A size_t size;
1N/A void *(*db_malloc) __P((size_t)), *storep;
1N/A{
1N/A void *p;
1N/A
1N/A *(void **)storep = NULL;
1N/A
1N/A /* Never allocate 0 bytes -- some C libraries don't like it. */
1N/A if (size == 0)
1N/A ++size;
1N/A
1N/A /* Some C libraries don't correctly set errno when malloc(3) fails. */
1N/A errno = 0;
1N/A if (db_malloc != NULL)
1N/A p = db_malloc(size);
1N/A else if (__db_jump.j_malloc != NULL)
1N/A p = __db_jump.j_malloc(size);
1N/A else
1N/A p = malloc(size);
1N/A if (p == NULL) {
1N/A if (errno == 0)
1N/A errno = ENOMEM;
1N/A return (errno);
1N/A }
1N/A
1N/A#ifdef DIAGNOSTIC
1N/A memset(p, 0xdb, size);
1N/A#endif
1N/A *(void **)storep = p;
1N/A
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * __os_realloc --
1N/A * The realloc(3) function for DB.
1N/A *
1N/A * PUBLIC: int __os_realloc __P((void *, size_t));
1N/A */
1N/Aint
1N/A__os_realloc(storep, size)
1N/A void *storep;
1N/A size_t size;
1N/A{
1N/A void *p, *ptr;
1N/A
1N/A ptr = *(void **)storep;
1N/A
1N/A /* If we haven't yet allocated anything yet, simply call malloc. */
1N/A if (ptr == NULL)
1N/A return (__os_malloc(size, NULL, storep));
1N/A
1N/A /* Never allocate 0 bytes -- some C libraries don't like it. */
1N/A if (size == 0)
1N/A ++size;
1N/A
1N/A /*
1N/A * Some C libraries don't correctly set errno when realloc(3) fails.
1N/A *
1N/A * Don't overwrite the original pointer, there are places in DB we
1N/A * try to continue after realloc fails.
1N/A */
1N/A errno = 0;
1N/A if (__db_jump.j_realloc != NULL)
1N/A p = __db_jump.j_realloc(ptr, size);
1N/A else
1N/A p = realloc(ptr, size);
1N/A if (p == NULL) {
1N/A if (errno == 0)
1N/A errno = ENOMEM;
1N/A return (errno);
1N/A }
1N/A
1N/A *(void **)storep = p;
1N/A
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * __os_free --
1N/A * The free(3) function for DB.
1N/A *
1N/A * PUBLIC: void __os_free __P((void *, size_t));
1N/A */
1N/Avoid
1N/A__os_free(ptr, size)
1N/A void *ptr;
1N/A size_t size;
1N/A{
1N/A#ifdef DIAGNOSTIC
1N/A if (size != 0)
1N/A memset(ptr, 0xdb, size);
1N/A#endif
1N/A
1N/A if (__db_jump.j_free != NULL)
1N/A __db_jump.j_free(ptr);
1N/A else
1N/A free(ptr);
1N/A}
1N/A
1N/A/*
1N/A * __os_freestr --
1N/A * The free(3) function for DB, freeing a string.
1N/A *
1N/A * PUBLIC: void __os_freestr __P((void *));
1N/A */
1N/Avoid
1N/A__os_freestr(ptr)
1N/A void *ptr;
1N/A{
1N/A#ifdef DIAGNOSTIC
1N/A memset(ptr, 0xdb, strlen(ptr) + 1);
1N/A#endif
1N/A
1N/A if (__db_jump.j_free != NULL)
1N/A __db_jump.j_free(ptr);
1N/A else
1N/A free(ptr);
1N/A}