summaryrefslogtreecommitdiffstats
path: root/src/lib/gssapi/generic/gssapi_alloc.h
blob: 9c0f340b1117867aad3b6cfa31d94516c8ebf622 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/* To the extent possible under law, Painless Security, LLC has waived
 * all copyright and related or neighboring rights to GSS-API Memory
 * Management Header. This work is published from: United States.
 */

#ifndef GSSAPI_ALLOC_H
#define GSSAPI_ALLOC_H

#ifdef _WIN32
#include "winbase.h"
#define USE_HEAPALLOC 1
#else
#define USE_HEAPALLOC 0
#endif
#include <string.h>

static inline void
gssalloc_free(void * value)
{
    if (value) {
#if USE_HEAPALLOC
        HeapFree(GetProcessHeap(), 0, value);
#else
        free(value);
#endif
    }
}

static inline void *
gssalloc_malloc(size_t size)
{
#if USE_HEAPALLOC
    return HeapAlloc(GetProcessHeap(), 0, size);
#else
    return malloc(size);
#endif
}

static inline void *
gssalloc_calloc(size_t count, size_t size)
{
#if USE_HEAPALLOC
    return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, count * size);
#else
    return calloc(count, size);
#endif
}

static inline void *
gssalloc_realloc(void *value, size_t size)
{
#if USE_HEAPALLOC
    return HeapReAlloc(GetProcessHeap(), 0, value, size);
#else
    return realloc(value, size);
#endif
}

static inline char *
gssalloc_strdup(const char *str)
{
    size_t size = strlen(str)+1;
    char *copy = gssalloc_malloc(size);
    if (copy) {
        memcpy(copy, str, size);
        copy[size-1] = '\0';
    }
    return copy;
}

#endif