-
Notifications
You must be signed in to change notification settings - Fork 22
/
alloc.h
68 lines (57 loc) · 1.5 KB
/
alloc.h
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
#ifndef __ALLOC_H
#define __ALLOC_H
#include <assert.h>
typedef NTSTATUS(WINAPI * _NtAllocateVirtualMemory)(
_In_ HANDLE ProcessHandle,
_Inout_ PVOID *BaseAddress,
_In_ ULONG_PTR ZeroBits,
_Inout_ PSIZE_T RegionSize,
_In_ ULONG AllocationType,
_In_ ULONG Protect);
typedef NTSTATUS(WINAPI * _NtFreeVirtualMemory)(
_In_ HANDLE ProcessHandle,
_Inout_ PVOID *BaseAddress,
_Inout_ PSIZE_T RegionSize,
_In_ ULONG FreeType);
extern _NtAllocateVirtualMemory pNtAllocateVirtualMemory;
extern _NtFreeVirtualMemory pNtFreeVirtualMemory;
#define USE_PRIVATE_HEAP
#ifdef USE_PRIVATE_HEAP
extern HANDLE g_heap;
#else
struct cm_alloc_header {
DWORD Magic;
SIZE_T Used;
SIZE_T Max;
};
#define CM_ALLOC_METASIZE (sizeof(struct cm_alloc_header))
#define GET_CM_ALLOC_HEADER(x) (struct cm_alloc_header *)((PCHAR)(x) - CM_ALLOC_METASIZE)
#define CM_ALLOC_MAGIC 0xdeadc01d
#endif
extern void *cm_alloc(size_t size);
extern void *cm_realloc(void *ptr, size_t size);
extern void cm_free(void *ptr);
#ifdef USE_PRIVATE_HEAP
extern void *cm_calloc(size_t count, size_t size);
#else
static __inline void *cm_calloc(size_t count, size_t size)
{
char *buf = cm_alloc(count * size);
if (buf)
memset(buf, 0, count * size);
return buf;
}
#endif
static __inline char *cm_strdup(const char *ptr)
{
char *buf = cm_alloc(strlen(ptr) + 1);
if (buf)
strcpy(buf, ptr);
return buf;
}
#define calloc cm_calloc
#define malloc cm_alloc
#define free cm_free
#define realloc cm_realloc
#define strdup cm_strdup
#endif