713c89537e945168fbcc605e39066576560b0d56
[krb5.git] / src / lib / gssapi / generic / gssapi_alloc.h
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* To the extent possible under law, Painless Security, LLC has waived
3  * all copyright and related or neighboring rights to GSS-API Memory
4  * Management Header. This work is published from: United States.
5  */
6
7 #ifndef GSSAPI_ALLOC_H
8 #define GSSAPI_ALLOC_H
9
10 #ifdef _WIN32
11 #include "winbase.h"
12 #endif
13 #include <string.h>
14
15 static inline void
16 gssalloc_free(void * value)
17 {
18     if (value) {
19 #if _WIN32
20         HeapFree(GetProcessHeap(), 0, value);
21 #else
22         free(value);
23 #endif
24     }
25 }
26
27 static inline void *
28 gssalloc_malloc(size_t size)
29 {
30 #if _WIN32
31     return HeapAlloc(GetProcessHeap(), 0, size);
32 #else
33     return malloc(size);
34 #endif
35 }
36
37 static inline void *
38 gssalloc_calloc(size_t count, size_t size)
39 {
40 #if _WIN32
41     return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, count * size);
42 #else
43     return calloc(count, size);
44 #endif
45 }
46
47 static inline void *
48 gssalloc_realloc(void *value, size_t size)
49 {
50 #if _WIN32
51     return HeapReAlloc(GetProcessHeap(), 0, value, size);
52 #else
53     return realloc(value, size);
54 #endif
55 }
56
57 static inline char *
58 gssalloc_strdup(const char *str)
59 {
60     size_t size = strlen(str)+1;
61     char *copy = gssalloc_malloc(size);
62     if (copy) {
63         memcpy(copy, str, size);
64         copy[size-1] = '\0';
65     }
66     return copy;
67 }
68
69 #endif