liboom 1.0.1
Debugging helper library with Memory leak detection
oomstub.c
Go to the documentation of this file.
1
23#include "liboom/oomstub.h"
24#include <stdlib.h> /* NULL */
25#include <dlfcn.h>
26
27#ifndef RTLD_NEXT
28#define RTLD_NEXT ((void *) -1L)
29#endif
30
36static int oomstub_countdown = 0;
37
42static void *(*real_malloc)(size_t);
43
48static void *(*real_calloc)(size_t, size_t);
49
54static void *(*real_realloc)(void *, size_t);
55
62void oomstub_setcountdown(const int counter) {
63 oomstub_countdown = counter;
64}
65
73 return oomstub_countdown;
74}
75
87void *malloc(size_t size) {
88 if (!real_malloc) {
89 *(void **) (&real_malloc) = dlsym(RTLD_NEXT, "malloc");
90 }
91 if (oomstub_countdown == 1) {
92 oomstub_countdown = 0;
93 return NULL;
94 }
95 if (oomstub_countdown > 0) {
96 oomstub_countdown--;
97 }
98 return real_malloc(size);
99}
100
113void *calloc(size_t count, size_t size) {
114 if (!real_calloc) {
115 *(void **) (&real_calloc) = dlsym(RTLD_NEXT, "calloc");
116 }
117 if (oomstub_countdown == 1) {
118 oomstub_countdown = 0;
119 return NULL;
120 }
121 if (oomstub_countdown > 0) {
122 oomstub_countdown--;
123 }
124 return real_calloc(count, size);
125}
126
153void *realloc(void *ptr, size_t new_size) {
154 if (!real_realloc) {
155 *(void **) (&real_realloc) = dlsym(RTLD_NEXT, "realloc");
156 }
157 if (oomstub_countdown == 1) {
158 oomstub_countdown = 0;
159 if (new_size > 0)
160 return NULL;
161 else
162 return ptr;
163 }
164 if (oomstub_countdown > 0) {
165 oomstub_countdown--;
166 }
167
168 return real_realloc(ptr, new_size);
169}
170
171/* vim: set tw=80: */
void * realloc(void *ptr, size_t new_size)
stdc realloc stub function
Definition: oomstub.c:153
int oomstub_getcountdown()
Gets the current malloc countdown before triggering a failure.
Definition: oomstub.c:72
#define RTLD_NEXT
Definition: oomstub.c:28
void * malloc(size_t size)
stdc malloc stub function
Definition: oomstub.c:87
void oomstub_setcountdown(const int counter)
Sets the malloc countdown before triggering a failure.
Definition: oomstub.c:62
void * calloc(size_t count, size_t size)
stdc calloc stub function
Definition: oomstub.c:113
C memory management diverters.