Fix no-param lambda environment bug.

Put allocation cap behind an ifdef
This commit is contained in:
Sage Vaillancourt 2022-03-19 21:28:17 -04:00
parent 9da4649a27
commit c9be701b19
3 changed files with 45 additions and 42 deletions

View File

@ -21,15 +21,13 @@ Object fetchFromEnvironment(const char* name, struct Environment* env)
} }
for (int i = 0; i < env->size; i++) { for (int i = 0; i < env->size; i++) {
if (env->strings[i] == NULL) { if (env->strings[i] == NULL) { printd("Try %d (NULL)\n", i);
printd("Try %d (NULL)\n", i);
break; break;
} }
printd("Try %d (%s)\n", i, env->strings[i]); printd("Try %d (%s)\n", i, env->strings[i]);
debugObj(&env->objects[i]); debugObj(&env->objects[i]);
if (strcmp(name, env->strings[i]) == 0) { if (strcmp(name, env->strings[i]) == 0) { printd("Returning!\n");
printd("Returning!\n");
return cloneObject(env->objects[i]); return cloneObject(env->objects[i]);
} }
} }
@ -47,6 +45,10 @@ struct Environment envForLambda(const Object* params, const Object* arg_forms,
{ {
int paramCount = listLength(params); int paramCount = listLength(params);
if (outer) {
outer->refs += 1;
}
struct Environment env = { struct Environment env = {
.outer = outer, .outer = outer,
.strings = NULL, .strings = NULL,
@ -58,7 +60,7 @@ struct Environment envForLambda(const Object* params, const Object* arg_forms,
}; };
if (paramCount == 0) { if (paramCount == 0) {
return env; return outer ? *outer : env;
} }
env.strings = calloc(sizeof(char*), paramCount + 1); env.strings = calloc(sizeof(char*), paramCount + 1);
@ -74,9 +76,6 @@ struct Environment envForLambda(const Object* params, const Object* arg_forms,
march = march->forward; march = march->forward;
} }
if(outer) {
outer->refs += 1;
}
return env; return env;
} }
@ -225,6 +224,7 @@ struct symFunc {
}; };
struct Environment* _global; struct Environment* _global;
struct Environment* global() struct Environment* global()
{ {
return _global; return _global;

View File

@ -5,6 +5,7 @@
#include <string.h> #include <string.h>
size_t bytes = 0; size_t bytes = 0;
int getBytes() int getBytes()
{ {
return bytes; return bytes;
@ -135,6 +136,7 @@ inline int isEmpty(const Object* obj)
} }
int allocations = 0; int allocations = 0;
int getAllocations() int getAllocations()
{ {
return allocations; return allocations;
@ -148,11 +150,13 @@ int getAllocations()
*/ */
void allocObject(Object** spot, const Object src) void allocObject(Object** spot, const Object src)
{ {
#ifdef ALLOCATION_CAP
if (allocations >= 10000) { if (allocations >= 10000) {
printf("MAX ALLOCATIONS EXCEEDED\n"); printf("MAX ALLOCATIONS EXCEEDED\n");
*spot = NULL; *spot = NULL;
return; return;
} }
#endif
allocations++; allocations++;
*spot = malloc(sizeof(struct Object)); *spot = malloc(sizeof(struct Object));
**spot = src; **spot = src;
@ -341,6 +345,7 @@ void stringStruct(char* dest, const Object* obj)
#else #else
#define stringf(_dest, _len, _format, args...) snprintf(_dest, _len, _format, ## args) #define stringf(_dest, _len, _format, args...) snprintf(_dest, _len, _format, ## args)
#endif #endif
char* stringNObj(char* dest, const Object* obj, const size_t len) char* stringNObj(char* dest, const Object* obj, const size_t len)
{ {
if (!dest || !obj) { if (!dest || !obj) {
@ -630,12 +635,10 @@ void deleteList(Object* dest)
void _copyList(Object* dest, const Object* src, int delete) void _copyList(Object* dest, const Object* src, int delete)
{ {
if (!dest || !src) { if (!dest || !src) { printd("NULL\n");
printd("NULL\n");
return; return;
} }
if (!isListy(*dest) || !isListy(*src)) { if (!isListy(*dest) || !isListy(*src)) { printd("NOT A LIST\n");
printd("NOT A LIST\n");
return; return;
} }

View File

@ -26,7 +26,8 @@ int requestDefinition = -1;
int routeCount = 0; int routeCount = 0;
struct Route routes[10]; struct Route routes[10];
int addRoute(struct Route route) { int addRoute(struct Route route)
{
routes[routeCount] = route; routes[routeCount] = route;
routeCount += 1; routeCount += 1;
return 0; return 0;
@ -89,8 +90,6 @@ answer_to_connection(void *cls, struct MHD_Connection *connection,
page = "<html><body><h1>404, Dumbass.</h1></body></html>"; page = "<html><body><h1>404, Dumbass.</h1></body></html>";
} }
struct MHD_Response *response;
HttpResult ret;
(void) cls; /* Unused. Silent compiler warning. */ (void) cls; /* Unused. Silent compiler warning. */
(void) url; /* Unused. Silent compiler warning. */ (void) url; /* Unused. Silent compiler warning. */
(void) method; /* Unused. Silent compiler warning. */ (void) method; /* Unused. Silent compiler warning. */
@ -99,10 +98,9 @@ answer_to_connection(void *cls, struct MHD_Connection *connection,
(void) upload_data_size; /* Unused. Silent compiler warning. */ (void) upload_data_size; /* Unused. Silent compiler warning. */
(void) con_cls; /* Unused. Silent compiler warning. */ (void) con_cls; /* Unused. Silent compiler warning. */
response = struct MHD_Response* response = MHD_create_response_from_buffer(
MHD_create_response_from_buffer(strlen(page), (void *) page, strlen(page), (void*) page, MHD_RESPMEM_PERSISTENT);
MHD_RESPMEM_PERSISTENT); HttpResult ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
MHD_destroy_response(response); MHD_destroy_response(response);
// if (needsFree) { // if (needsFree) {
@ -113,6 +111,7 @@ answer_to_connection(void *cls, struct MHD_Connection *connection,
} }
int initialized = 0; int initialized = 0;
void initialize() void initialize()
{ {
if (!initialized) { if (!initialized) {
@ -130,8 +129,9 @@ int start(int port)
MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD, MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD,
port, NULL, NULL, &answer_to_connection, NULL, MHD_OPTION_END); port, NULL, NULL, &answer_to_connection, NULL, MHD_OPTION_END);
if (NULL == daemon) if (NULL == daemon) {
return 1; return 1;
}
// (void) getchar(); // (void) getchar();