diff options
author | Nils Durner <durner@gnunet.org> | 2009-11-09 13:24:50 +0000 |
---|---|---|
committer | Nils Durner <durner@gnunet.org> | 2009-11-09 13:24:50 +0000 |
commit | a6ed6706bc790380caabdde179877f1589867e0b (patch) | |
tree | 212640da556fb01bd58435b479bc4b85ca61d725 /src/util/common_allocation.c | |
parent | 162b79c21d0c0e898c5dd471ef50e51f259791aa (diff) | |
download | gnunet-a6ed6706bc790380caabdde179877f1589867e0b.tar.gz gnunet-a6ed6706bc790380caabdde179877f1589867e0b.zip |
hard memory limit for safe buildbot'ing on Windows
Diffstat (limited to 'src/util/common_allocation.c')
-rw-r--r-- | src/util/common_allocation.c | 41 |
1 files changed, 39 insertions, 2 deletions
diff --git a/src/util/common_allocation.c b/src/util/common_allocation.c index 748ca620d..4c1d53c2d 100644 --- a/src/util/common_allocation.c +++ b/src/util/common_allocation.c | |||
@@ -31,6 +31,14 @@ | |||
31 | #define INT_MAX 0x7FFFFFFF | 31 | #define INT_MAX 0x7FFFFFFF |
32 | #endif | 32 | #endif |
33 | 33 | ||
34 | #ifdef MINGW | ||
35 | #define W32_MEM_LIMIT 200000000 | ||
36 | #endif | ||
37 | |||
38 | #ifdef W32_MEM_LIMIT | ||
39 | static LONG mem_used = 0; | ||
40 | #endif | ||
41 | |||
34 | /** | 42 | /** |
35 | * Allocate memory. Checks the return value, aborts if no more | 43 | * Allocate memory. Checks the return value, aborts if no more |
36 | * memory is available. | 44 | * memory is available. |
@@ -57,6 +65,12 @@ GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber) | |||
57 | { | 65 | { |
58 | void *result; | 66 | void *result; |
59 | 67 | ||
68 | #ifdef W32_MEM_LIMIT | ||
69 | size += sizeof(size_t); | ||
70 | if (mem_used + size > W32_MEM_LIMIT) | ||
71 | return NULL; | ||
72 | #endif | ||
73 | |||
60 | GNUNET_assert_at (size < INT_MAX, filename, linenumber); | 74 | GNUNET_assert_at (size < INT_MAX, filename, linenumber); |
61 | result = malloc (size); | 75 | result = malloc (size); |
62 | if (result == NULL) | 76 | if (result == NULL) |
@@ -65,6 +79,13 @@ GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber) | |||
65 | abort (); | 79 | abort (); |
66 | } | 80 | } |
67 | memset (result, 0, size); | 81 | memset (result, 0, size); |
82 | |||
83 | #ifdef W32_MEM_LIMIT | ||
84 | *((size_t *) result) = size; | ||
85 | result = &((size_t *) result)[1]; | ||
86 | mem_used += size; | ||
87 | #endif | ||
88 | |||
68 | return result; | 89 | return result; |
69 | } | 90 | } |
70 | 91 | ||
@@ -80,14 +101,27 @@ GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber) | |||
80 | */ | 101 | */ |
81 | void * | 102 | void * |
82 | GNUNET_xrealloc_ (void *ptr, | 103 | GNUNET_xrealloc_ (void *ptr, |
83 | const size_t n, const char *filename, int linenumber) | 104 | #ifndef W32_MEM_LIMIT |
105 | const size_t n, | ||
106 | #else | ||
107 | size_t n, | ||
108 | #endif | ||
109 | const char *filename, int linenumber) | ||
84 | { | 110 | { |
111 | #ifdef W32_MEM_LIMIT | ||
112 | n += sizeof(size_t); | ||
113 | ptr = &((size_t *) ptr)[-1]; | ||
114 | mem_used = mem_used - *((size_t *) ptr) + n; | ||
115 | #endif | ||
85 | ptr = realloc (ptr, n); | 116 | ptr = realloc (ptr, n); |
86 | if (!ptr) | 117 | if (!ptr) |
87 | { | 118 | { |
88 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "realloc"); | 119 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "realloc"); |
89 | abort (); | 120 | abort (); |
90 | } | 121 | } |
122 | #ifdef W32_MEM_LIMIT | ||
123 | ptr = &((size_t *) ptr)[1]; | ||
124 | #endif | ||
91 | return ptr; | 125 | return ptr; |
92 | } | 126 | } |
93 | 127 | ||
@@ -103,6 +137,10 @@ void | |||
103 | GNUNET_xfree_ (void *ptr, const char *filename, int linenumber) | 137 | GNUNET_xfree_ (void *ptr, const char *filename, int linenumber) |
104 | { | 138 | { |
105 | GNUNET_assert_at (ptr != NULL, filename, linenumber); | 139 | GNUNET_assert_at (ptr != NULL, filename, linenumber); |
140 | #ifdef W32_MEM_LIMIT | ||
141 | ptr = &((size_t *) ptr)[-1]; | ||
142 | mem_used -= *((size_t *) ptr); | ||
143 | #endif | ||
106 | free (ptr); | 144 | free (ptr); |
107 | } | 145 | } |
108 | 146 | ||
@@ -217,5 +255,4 @@ GNUNET_snprintf (char *buf, size_t size, const char *format, ...) | |||
217 | return ret; | 255 | return ret; |
218 | } | 256 | } |
219 | 257 | ||
220 | |||
221 | /* end of common_allocation.c */ | 258 | /* end of common_allocation.c */ |