-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.h
98 lines (83 loc) · 2.46 KB
/
memory.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* This file is part of John the Ripper password cracker,
* Copyright (c) 1996-98,2003,2010-2012,2016 by Solar Designer
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted.
*
* There's ABSOLUTELY NO WARRANTY, express or implied.
*/
/*
* Memory allocation routines.
*/
#ifndef _JOHN_MEMORY_H
#define _JOHN_MEMORY_H
#include <stdio.h>
#include <stdlib.h>
#include "arch.h"
/*
* Standard alignments for mem_alloc_tiny().
*/
#define MEM_ALIGN_NONE 1
#define MEM_ALIGN_WORD ARCH_SIZE
/*
* These are hopefully suitable guesses. They are right for only a subset of
* the architectures/CPUs we support, yet our use of them does not require that
* they be entirely correct.
*/
#define MEM_ALIGN_CACHE (ARCH_SIZE * 8)
#define MEM_ALIGN_PAGE 0x1000
/*
* Block size used by mem_alloc_tiny().
*/
#define MEM_ALLOC_SIZE 0x10000
/*
* Use mem_alloc() instead of allocating a new block in mem_alloc_tiny()
* if more than MEM_ALLOC_MAX_WASTE bytes would be lost.
* This shouldn't be set too small, or mem_alloc_tiny() will keep calling
* mem_alloc() for many allocations in a row, which might end up wasting even
* more memory to malloc() overhead.
*/
#define MEM_ALLOC_MAX_WASTE 0xff
/*
* Memory saving level, setting this high enough disables alignments (if the
* architecture allows).
*/
extern unsigned int mem_saving_level;
/*
* Allocates size bytes and returns a pointer to the allocated memory, or NULL
* if size is 0.
* If an error occurs, the function does not return.
*/
extern void *mem_alloc(size_t size);
/*
* Allocates nmemb*size bytes using calloc(3) and returns a pointer to the
* allocated memory, or NULL if nmemb or/and size are 0.
* If an error occurs, the function does not return.
*/
extern void *mem_calloc(size_t nmemb, size_t size);
/*
* Frees memory allocated with mem_alloc() and sets the pointer to NULL.
* Does nothing if the pointer is already NULL.
*/
#define MEM_FREE(ptr) \
{ \
if ((ptr)) { \
free((ptr)); \
(ptr) = NULL; \
} \
}
/*
* Similar to the above function, except the memory can't be freed.
* This one is used to reduce the overhead.
*/
extern void *mem_alloc_tiny(size_t size, size_t align);
/*
* Uses mem_alloc_tiny() to allocate the memory, and copies src in there.
*/
extern void *mem_alloc_copy(const void *src, size_t size, size_t align);
/*
* Similar to the above function, but for ASCIIZ strings.
*/
extern char *str_alloc_copy(const char *src);
#endif