Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 10:18:33

0001 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
0002 /* To the extent possible under law, Painless Security, LLC has waived
0003  * all copyright and related or neighboring rights to GSS-API Memory
0004  * Management Header. This work is published from: United States.
0005  */
0006 
0007 #ifndef GSSAPI_ALLOC_H
0008 #define GSSAPI_ALLOC_H
0009 
0010 #ifdef _WIN32
0011 #include "winbase.h"
0012 #endif
0013 #include <string.h>
0014 
0015 #if defined(_WIN32)
0016 
0017 static inline void
0018 gssalloc_free(void *value)
0019 {
0020     if (value)
0021         HeapFree(GetProcessHeap(), 0, value);
0022 }
0023 
0024 static inline void *
0025 gssalloc_malloc(size_t size)
0026 {
0027     return HeapAlloc(GetProcessHeap(), 0, size);
0028 }
0029 
0030 static inline void *
0031 gssalloc_calloc(size_t count, size_t size)
0032 {
0033     return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, count * size);
0034 }
0035 
0036 static inline void *
0037 gssalloc_realloc(void *value, size_t size)
0038 {
0039     /* Unlike realloc(), HeapReAlloc() does not work on null values. */
0040     if (value == NULL)
0041         return HeapAlloc(GetProcessHeap(), 0, size);
0042     return HeapReAlloc(GetProcessHeap(), 0, value, size);
0043 }
0044 
0045 #elif defined(DEBUG_GSSALLOC)
0046 
0047 /* Be deliberately incompatible with malloc and free, to allow us to detect
0048  * mismatched malloc/gssalloc usage on Unix. */
0049 
0050 static inline void
0051 gssalloc_free(void *value)
0052 {
0053     char *p = (char *)value - 8;
0054 
0055     if (value == NULL)
0056         return;
0057     if (memcmp(p, "gssalloc", 8) != 0)
0058         abort();
0059     free(p);
0060 }
0061 
0062 static inline void *
0063 gssalloc_malloc(size_t size)
0064 {
0065     char *p = calloc(size + 8, 1);
0066 
0067     memcpy(p, "gssalloc", 8);
0068     return p + 8;
0069 }
0070 
0071 static inline void *
0072 gssalloc_calloc(size_t count, size_t size)
0073 {
0074     return gssalloc_malloc(count * size);
0075 }
0076 
0077 static inline void *
0078 gssalloc_realloc(void *value, size_t size)
0079 {
0080     char *p = (char *)value - 8;
0081 
0082     if (value == NULL)
0083         return gssalloc_malloc(size);
0084     if (memcmp(p, "gssalloc", 8) != 0)
0085         abort();
0086     return (char *)realloc(p, size + 8) + 8;
0087 }
0088 
0089 #else /* not _WIN32 or DEBUG_GSSALLOC */
0090 
0091 /* Normal Unix case, just use free/malloc/calloc/realloc. */
0092 
0093 static inline void
0094 gssalloc_free(void *value)
0095 {
0096     free(value);
0097 }
0098 
0099 static inline void *
0100 gssalloc_malloc(size_t size)
0101 {
0102     return malloc(size);
0103 }
0104 
0105 static inline void *
0106 gssalloc_calloc(size_t count, size_t size)
0107 {
0108     return calloc(count, size);
0109 }
0110 
0111 static inline void *
0112 gssalloc_realloc(void *value, size_t size)
0113 {
0114     return realloc(value, size);
0115 }
0116 
0117 #endif /* not _WIN32 or DEBUG_GSSALLOC */
0118 
0119 static inline char *
0120 gssalloc_strdup(const char *str)
0121 {
0122     size_t size = strlen(str)+1;
0123     char *copy = gssalloc_malloc(size);
0124     if (copy) {
0125         memcpy(copy, str, size);
0126         copy[size-1] = '\0';
0127     }
0128     return copy;
0129 }
0130 
0131 #endif