summaryrefslogtreecommitdiffstats
path: root/src/lib/gssapi/generic/gssapi_alloc.h
diff options
context:
space:
mode:
authorSam Hartman <hartmans@mit.edu>2011-10-14 14:39:01 +0000
committerSam Hartman <hartmans@mit.edu>2011-10-14 14:39:01 +0000
commit9493aefa8abc949ec83792de8039f09f6d664c50 (patch)
tree801c01378beaee43a7c5a64b7a3951291d2b72f1 /src/lib/gssapi/generic/gssapi_alloc.h
parent8fd620fe538f33164e4faa395573d6739aa829a2 (diff)
downloadkrb5-9493aefa8abc949ec83792de8039f09f6d664c50.tar.gz
krb5-9493aefa8abc949ec83792de8039f09f6d664c50.tar.xz
krb5-9493aefa8abc949ec83792de8039f09f6d664c50.zip
Add new header gssapi_alloc.h
Contains allocator methods for use with mechanisms and mechglues for allocations that must be made in one module but freed in another. On windows, an allocation made in one module cannot safely be freed in another using the usual c runtime malloc/free; runtime dll mismatch will cause heap corruption in that case. But it is safe to instead directly use HeapAlloc()/HeapFree() specifying the default process heap. For now, this header is not public. If it becomes public strncpy will need to be used instead of strlcpy. Signed-off-by: Kevin Wasserman <kevin.wasserman@painless-security.com> git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@25330 dc483132-0cff-0310-8789-dd5450dbe970
Diffstat (limited to 'src/lib/gssapi/generic/gssapi_alloc.h')
-rw-r--r--src/lib/gssapi/generic/gssapi_alloc.h62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/lib/gssapi/generic/gssapi_alloc.h b/src/lib/gssapi/generic/gssapi_alloc.h
new file mode 100644
index 000000000..cccbdbb4c
--- /dev/null
+++ b/src/lib/gssapi/generic/gssapi_alloc.h
@@ -0,0 +1,62 @@
+/* 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"
+#endif
+#include <string.h>
+/*
+ * Note that we'll need to do something else if we decide to install
+ * this header for mechanisms.
+ */
+#include <k5-platform.h>
+
+static inline void
+gssalloc_free(void * value)
+{
+ if (value) {
+#if _WIN32
+ HeapFree(GetProcessHeap(), 0, value);
+#else
+ free(value);
+#endif
+ }
+}
+
+static inline void *
+gssalloc_malloc(size_t size)
+{
+#if _WIN32
+ return HeapAlloc(GetProcessHeap(), 0, size);
+#else
+ return malloc(size);
+#endif
+}
+
+static inline void *
+gssalloc_calloc(size_t count, size_t size)
+{
+#if _WIN32
+ return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, count * size);
+#else
+ return calloc(count, size);
+#endif
+}
+
+static inline char *
+gssalloc_strdup(const char *str)
+{
+ int size = strlen(str)+1;
+ char *copy = gssalloc_malloc(size);
+ if (copy) {
+ strlcpy(copy, str, size);
+ }
+ return copy;
+}
+
+#endif