Skip to content

Commit f8b4ada

Browse files
committed
THAlloc/THRealloc attempt lua GC on failure
1 parent e9e849a commit f8b4ada

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

THGeneral.c

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ static void defaultTorchErrorHandlerFunction(const char *msg, void *data)
1313
static __thread void (*torchErrorHandlerFunction)(const char *msg, void *data) = defaultTorchErrorHandlerFunction;
1414
static __thread void *torchErrorHandlerData;
1515

16+
static __thread void (*torchGCFunction)(void *data) = NULL;
17+
static __thread void *torchGCData;
18+
1619
void _THError(const char *file, const int line, const char *fmt, ...)
1720
{
1821
char msg[2048];
@@ -91,15 +94,15 @@ void THSetArgErrorHandler( void (*torchArgErrorHandlerFunction_)(int argNumber,
9194
torchArgErrorHandlerData = data;
9295
}
9396

94-
void* THAlloc(long size)
97+
void THSetGCHandler( void (*torchGCFunction_)(void *data), void *data )
9598
{
96-
void *ptr;
97-
98-
if(size < 0)
99-
THError("$ Torch: invalid memory size -- maybe an overflow?");
99+
torchGCFunction = torchGCFunction_;
100+
torchGCData = data;
101+
}
100102

101-
if(size == 0)
102-
return NULL;
103+
void* THAllocInternal(long size)
104+
{
105+
void *ptr;
103106

104107
if (size > 5120)
105108
{
@@ -116,6 +119,25 @@ void* THAlloc(long size)
116119
{
117120
ptr = malloc(size);
118121
}
122+
return ptr;
123+
}
124+
125+
void* THAlloc(long size)
126+
{
127+
void *ptr;
128+
129+
if(size < 0)
130+
THError("$ Torch: invalid memory size -- maybe an overflow?");
131+
132+
if(size == 0)
133+
return NULL;
134+
135+
ptr = THAllocInternal(size);
136+
137+
if(!ptr && torchGCFunction) {
138+
torchGCFunction(torchGCData);
139+
ptr = THAllocInternal(size);
140+
}
119141

120142
if(!ptr)
121143
THError("$ Torch: not enough memory: you tried to allocate %dGB. Buy new RAM!", size/1073741824);
@@ -137,10 +159,17 @@ void* THRealloc(void *ptr, long size)
137159
if(size < 0)
138160
THError("$ Torch: invalid memory size -- maybe an overflow?");
139161

140-
ptr = realloc(ptr, size);
141-
if(!ptr)
162+
void *newptr = realloc(ptr, size);
163+
164+
if(!newptr && torchGCFunction) {
165+
torchGCFunction(torchGCData);
166+
newptr = realloc(ptr, size);
167+
}
168+
169+
if(!newptr)
142170
THError("$ Torch: not enough memory: you tried to reallocate %dGB. Buy new RAM!", size/1073741824);
143-
return ptr;
171+
172+
return newptr;
144173
}
145174

146175
void THFree(void *ptr)

THGeneral.h.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ TH_API void THSetArgErrorHandler( void (*torchArgErrorHandlerFunction)(int argNu
5252
TH_API void* THAlloc(long size);
5353
TH_API void* THRealloc(void *ptr, long size);
5454
TH_API void THFree(void *ptr);
55+
TH_API void THSetGCHandler( void (*torchGCHandlerFunction)(void *data), void *data );
5556

5657
#define THError(...) _THError(__FILE__, __LINE__, __VA_ARGS__)
5758
#define THArgCheck(...) _THArgCheck(__FILE__, __LINE__, __VA_ARGS__)

0 commit comments

Comments
 (0)