Path: blob/main/crypto/krb5/src/lib/gssapi/generic/gssapi_alloc.h
39563 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* To the extent possible under law, Painless Security, LLC has waived2* all copyright and related or neighboring rights to GSS-API Memory3* Management Header. This work is published from: United States.4*/56#ifndef GSSAPI_ALLOC_H7#define GSSAPI_ALLOC_H89#ifdef _WIN3210#include "winbase.h"11#endif12#include <string.h>1314#if defined(_WIN32)1516static inline void17gssalloc_free(void *value)18{19if (value)20HeapFree(GetProcessHeap(), 0, value);21}2223static inline void *24gssalloc_malloc(size_t size)25{26return HeapAlloc(GetProcessHeap(), 0, size);27}2829static inline void *30gssalloc_calloc(size_t count, size_t size)31{32return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, count * size);33}3435static inline void *36gssalloc_realloc(void *value, size_t size)37{38/* Unlike realloc(), HeapReAlloc() does not work on null values. */39if (value == NULL)40return HeapAlloc(GetProcessHeap(), 0, size);41return HeapReAlloc(GetProcessHeap(), 0, value, size);42}4344#elif defined(DEBUG_GSSALLOC)4546/* Be deliberately incompatible with malloc and free, to allow us to detect47* mismatched malloc/gssalloc usage on Unix. */4849static inline void50gssalloc_free(void *value)51{52char *p = (char *)value - 8;5354if (value == NULL)55return;56if (memcmp(p, "gssalloc", 8) != 0)57abort();58free(p);59}6061static inline void *62gssalloc_malloc(size_t size)63{64char *p = calloc(size + 8, 1);6566memcpy(p, "gssalloc", 8);67return p + 8;68}6970static inline void *71gssalloc_calloc(size_t count, size_t size)72{73return gssalloc_malloc(count * size);74}7576static inline void *77gssalloc_realloc(void *value, size_t size)78{79char *p = (char *)value - 8;8081if (value == NULL)82return gssalloc_malloc(size);83if (memcmp(p, "gssalloc", 8) != 0)84abort();85return (char *)realloc(p, size + 8) + 8;86}8788#else /* not _WIN32 or DEBUG_GSSALLOC */8990/* Normal Unix case, just use free/malloc/calloc/realloc. */9192static inline void93gssalloc_free(void *value)94{95free(value);96}9798static inline void *99gssalloc_malloc(size_t size)100{101return malloc(size);102}103104static inline void *105gssalloc_calloc(size_t count, size_t size)106{107return calloc(count, size);108}109110static inline void *111gssalloc_realloc(void *value, size_t size)112{113return realloc(value, size);114}115116#endif /* not _WIN32 or DEBUG_GSSALLOC */117118static inline char *119gssalloc_strdup(const char *str)120{121size_t size = strlen(str)+1;122char *copy = gssalloc_malloc(size);123if (copy) {124memcpy(copy, str, size);125copy[size-1] = '\0';126}127return copy;128}129130#endif131132133