Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/crypto/krb5/src/lib/gssapi/generic/gssapi_alloc.h
39563 views
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
#if defined(_WIN32)
16
17
static inline void
18
gssalloc_free(void *value)
19
{
20
if (value)
21
HeapFree(GetProcessHeap(), 0, value);
22
}
23
24
static inline void *
25
gssalloc_malloc(size_t size)
26
{
27
return HeapAlloc(GetProcessHeap(), 0, size);
28
}
29
30
static inline void *
31
gssalloc_calloc(size_t count, size_t size)
32
{
33
return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, count * size);
34
}
35
36
static inline void *
37
gssalloc_realloc(void *value, size_t size)
38
{
39
/* Unlike realloc(), HeapReAlloc() does not work on null values. */
40
if (value == NULL)
41
return HeapAlloc(GetProcessHeap(), 0, size);
42
return HeapReAlloc(GetProcessHeap(), 0, value, size);
43
}
44
45
#elif defined(DEBUG_GSSALLOC)
46
47
/* Be deliberately incompatible with malloc and free, to allow us to detect
48
* mismatched malloc/gssalloc usage on Unix. */
49
50
static inline void
51
gssalloc_free(void *value)
52
{
53
char *p = (char *)value - 8;
54
55
if (value == NULL)
56
return;
57
if (memcmp(p, "gssalloc", 8) != 0)
58
abort();
59
free(p);
60
}
61
62
static inline void *
63
gssalloc_malloc(size_t size)
64
{
65
char *p = calloc(size + 8, 1);
66
67
memcpy(p, "gssalloc", 8);
68
return p + 8;
69
}
70
71
static inline void *
72
gssalloc_calloc(size_t count, size_t size)
73
{
74
return gssalloc_malloc(count * size);
75
}
76
77
static inline void *
78
gssalloc_realloc(void *value, size_t size)
79
{
80
char *p = (char *)value - 8;
81
82
if (value == NULL)
83
return gssalloc_malloc(size);
84
if (memcmp(p, "gssalloc", 8) != 0)
85
abort();
86
return (char *)realloc(p, size + 8) + 8;
87
}
88
89
#else /* not _WIN32 or DEBUG_GSSALLOC */
90
91
/* Normal Unix case, just use free/malloc/calloc/realloc. */
92
93
static inline void
94
gssalloc_free(void *value)
95
{
96
free(value);
97
}
98
99
static inline void *
100
gssalloc_malloc(size_t size)
101
{
102
return malloc(size);
103
}
104
105
static inline void *
106
gssalloc_calloc(size_t count, size_t size)
107
{
108
return calloc(count, size);
109
}
110
111
static inline void *
112
gssalloc_realloc(void *value, size_t size)
113
{
114
return realloc(value, size);
115
}
116
117
#endif /* not _WIN32 or DEBUG_GSSALLOC */
118
119
static inline char *
120
gssalloc_strdup(const char *str)
121
{
122
size_t size = strlen(str)+1;
123
char *copy = gssalloc_malloc(size);
124
if (copy) {
125
memcpy(copy, str, size);
126
copy[size-1] = '\0';
127
}
128
return copy;
129
}
130
131
#endif
132
133