diff --git a/src/include/hlib.h b/src/include/hlib.h index 1e94acb..d36b727 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -32,5 +32,6 @@ extern uintptr_t insertString(char *string, uintptr_t size); extern uintptr_t getStringLength(uintptr_t stringId); extern void readString(uintptr_t stringId, void *buffer); +extern void discardString(uintptr_t stringId); #endif diff --git a/src/include/hlib.h b/src/include/hlib.h index 1e94acb..d36b727 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -32,5 +32,6 @@ extern uintptr_t insertString(char *string, uintptr_t size); extern uintptr_t getStringLength(uintptr_t stringId); extern void readString(uintptr_t stringId, void *buffer); +extern void discardString(uintptr_t stringId); #endif diff --git a/src/kernel/stringmap/stringmap.c b/src/kernel/stringmap/stringmap.c index 6e0cea1..44a287f 100644 --- a/src/kernel/stringmap/stringmap.c +++ b/src/kernel/stringmap/stringmap.c @@ -47,13 +47,13 @@ void discardString(uintptr_t stringId) { MapLayer currentLayer = rootLayer; - for (uint32_t startBit = 0; startBit < sizeof(uintptr_t) - 4; - startBit += 4) { + for (uint32_t startBit = 0; startBit < BITS(uintptr_t) - 4; startBit += 4) { void *nextLayer = currentLayer[(stringId >> startBit) & 0xF]; if (!nextLayer) { return; } currentLayer = nextLayer; } - currentLayer[stringId >> (sizeof(uintptr_t) - 4)] = NULL; + free(currentLayer[stringId >> (BITS(uintptr_t) - 4)]); + currentLayer[stringId >> (BITS(uintptr_t) - 4)] = NULL; } diff --git a/src/include/hlib.h b/src/include/hlib.h index 1e94acb..d36b727 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -32,5 +32,6 @@ extern uintptr_t insertString(char *string, uintptr_t size); extern uintptr_t getStringLength(uintptr_t stringId); extern void readString(uintptr_t stringId, void *buffer); +extern void discardString(uintptr_t stringId); #endif diff --git a/src/kernel/stringmap/stringmap.c b/src/kernel/stringmap/stringmap.c index 6e0cea1..44a287f 100644 --- a/src/kernel/stringmap/stringmap.c +++ b/src/kernel/stringmap/stringmap.c @@ -47,13 +47,13 @@ void discardString(uintptr_t stringId) { MapLayer currentLayer = rootLayer; - for (uint32_t startBit = 0; startBit < sizeof(uintptr_t) - 4; - startBit += 4) { + for (uint32_t startBit = 0; startBit < BITS(uintptr_t) - 4; startBit += 4) { void *nextLayer = currentLayer[(stringId >> startBit) & 0xF]; if (!nextLayer) { return; } currentLayer = nextLayer; } - currentLayer[stringId >> (sizeof(uintptr_t) - 4)] = NULL; + free(currentLayer[stringId >> (BITS(uintptr_t) - 4)]); + currentLayer[stringId >> (BITS(uintptr_t) - 4)] = NULL; } diff --git a/src/kernel/stringmap/stringmapSyscalls.c b/src/kernel/stringmap/stringmapSyscalls.c index 9ee8c6f..33e3d4e 100644 --- a/src/kernel/stringmap/stringmapSyscalls.c +++ b/src/kernel/stringmap/stringmapSyscalls.c @@ -29,3 +29,8 @@ memcpy(string, buffer, size + 1); unmapPage(buffer); } + +void handleDiscardStringSyscall(Syscall *call) { + uintptr_t stringId = call->parameters[0]; + discardString(stringId); +} diff --git a/src/include/hlib.h b/src/include/hlib.h index 1e94acb..d36b727 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -32,5 +32,6 @@ extern uintptr_t insertString(char *string, uintptr_t size); extern uintptr_t getStringLength(uintptr_t stringId); extern void readString(uintptr_t stringId, void *buffer); +extern void discardString(uintptr_t stringId); #endif diff --git a/src/kernel/stringmap/stringmap.c b/src/kernel/stringmap/stringmap.c index 6e0cea1..44a287f 100644 --- a/src/kernel/stringmap/stringmap.c +++ b/src/kernel/stringmap/stringmap.c @@ -47,13 +47,13 @@ void discardString(uintptr_t stringId) { MapLayer currentLayer = rootLayer; - for (uint32_t startBit = 0; startBit < sizeof(uintptr_t) - 4; - startBit += 4) { + for (uint32_t startBit = 0; startBit < BITS(uintptr_t) - 4; startBit += 4) { void *nextLayer = currentLayer[(stringId >> startBit) & 0xF]; if (!nextLayer) { return; } currentLayer = nextLayer; } - currentLayer[stringId >> (sizeof(uintptr_t) - 4)] = NULL; + free(currentLayer[stringId >> (BITS(uintptr_t) - 4)]); + currentLayer[stringId >> (BITS(uintptr_t) - 4)] = NULL; } diff --git a/src/kernel/stringmap/stringmapSyscalls.c b/src/kernel/stringmap/stringmapSyscalls.c index 9ee8c6f..33e3d4e 100644 --- a/src/kernel/stringmap/stringmapSyscalls.c +++ b/src/kernel/stringmap/stringmapSyscalls.c @@ -29,3 +29,8 @@ memcpy(string, buffer, size + 1); unmapPage(buffer); } + +void handleDiscardStringSyscall(Syscall *call) { + uintptr_t stringId = call->parameters[0]; + discardString(stringId); +} diff --git a/src/kernel/syscalls/syscall.c b/src/kernel/syscalls/syscall.c index f1cb269..abe9808 100644 --- a/src/kernel/syscalls/syscall.c +++ b/src/kernel/syscalls/syscall.c @@ -57,7 +57,7 @@ handleFireEventSyscall, handleSubscribeEventSyscall; extern uintptr_t handleSubscribeInterruptSyscall; extern uintptr_t handleInsertStringSyscall, handleReadStringLengthSyscall, - handleReadStringSyscall; + handleReadStringSyscall, handleDiscardStringSyscall; void (*syscallHandlers[])(Syscall *) = { 0, @@ -77,6 +77,7 @@ (void *)&handleInsertStringSyscall, (void *)&handleReadStringLengthSyscall, (void *)&handleReadStringSyscall, + (void *)&handleDiscardStringSyscall, }; void processSyscall(Syscall *call) { diff --git a/src/include/hlib.h b/src/include/hlib.h index 1e94acb..d36b727 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -32,5 +32,6 @@ extern uintptr_t insertString(char *string, uintptr_t size); extern uintptr_t getStringLength(uintptr_t stringId); extern void readString(uintptr_t stringId, void *buffer); +extern void discardString(uintptr_t stringId); #endif diff --git a/src/kernel/stringmap/stringmap.c b/src/kernel/stringmap/stringmap.c index 6e0cea1..44a287f 100644 --- a/src/kernel/stringmap/stringmap.c +++ b/src/kernel/stringmap/stringmap.c @@ -47,13 +47,13 @@ void discardString(uintptr_t stringId) { MapLayer currentLayer = rootLayer; - for (uint32_t startBit = 0; startBit < sizeof(uintptr_t) - 4; - startBit += 4) { + for (uint32_t startBit = 0; startBit < BITS(uintptr_t) - 4; startBit += 4) { void *nextLayer = currentLayer[(stringId >> startBit) & 0xF]; if (!nextLayer) { return; } currentLayer = nextLayer; } - currentLayer[stringId >> (sizeof(uintptr_t) - 4)] = NULL; + free(currentLayer[stringId >> (BITS(uintptr_t) - 4)]); + currentLayer[stringId >> (BITS(uintptr_t) - 4)] = NULL; } diff --git a/src/kernel/stringmap/stringmapSyscalls.c b/src/kernel/stringmap/stringmapSyscalls.c index 9ee8c6f..33e3d4e 100644 --- a/src/kernel/stringmap/stringmapSyscalls.c +++ b/src/kernel/stringmap/stringmapSyscalls.c @@ -29,3 +29,8 @@ memcpy(string, buffer, size + 1); unmapPage(buffer); } + +void handleDiscardStringSyscall(Syscall *call) { + uintptr_t stringId = call->parameters[0]; + discardString(stringId); +} diff --git a/src/kernel/syscalls/syscall.c b/src/kernel/syscalls/syscall.c index f1cb269..abe9808 100644 --- a/src/kernel/syscalls/syscall.c +++ b/src/kernel/syscalls/syscall.c @@ -57,7 +57,7 @@ handleFireEventSyscall, handleSubscribeEventSyscall; extern uintptr_t handleSubscribeInterruptSyscall; extern uintptr_t handleInsertStringSyscall, handleReadStringLengthSyscall, - handleReadStringSyscall; + handleReadStringSyscall, handleDiscardStringSyscall; void (*syscallHandlers[])(Syscall *) = { 0, @@ -77,6 +77,7 @@ (void *)&handleInsertStringSyscall, (void *)&handleReadStringLengthSyscall, (void *)&handleReadStringSyscall, + (void *)&handleDiscardStringSyscall, }; void processSyscall(Syscall *call) { diff --git a/src/kernel/util/strings.c b/src/kernel/util/strings.c index b023bcb..324a58e 100644 --- a/src/kernel/util/strings.c +++ b/src/kernel/util/strings.c @@ -13,6 +13,9 @@ } uint32_t strlen(char *string) { + if (!string) { + return 0; + } uint32_t size = 0; while (*string) { string++; diff --git a/src/include/hlib.h b/src/include/hlib.h index 1e94acb..d36b727 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -32,5 +32,6 @@ extern uintptr_t insertString(char *string, uintptr_t size); extern uintptr_t getStringLength(uintptr_t stringId); extern void readString(uintptr_t stringId, void *buffer); +extern void discardString(uintptr_t stringId); #endif diff --git a/src/kernel/stringmap/stringmap.c b/src/kernel/stringmap/stringmap.c index 6e0cea1..44a287f 100644 --- a/src/kernel/stringmap/stringmap.c +++ b/src/kernel/stringmap/stringmap.c @@ -47,13 +47,13 @@ void discardString(uintptr_t stringId) { MapLayer currentLayer = rootLayer; - for (uint32_t startBit = 0; startBit < sizeof(uintptr_t) - 4; - startBit += 4) { + for (uint32_t startBit = 0; startBit < BITS(uintptr_t) - 4; startBit += 4) { void *nextLayer = currentLayer[(stringId >> startBit) & 0xF]; if (!nextLayer) { return; } currentLayer = nextLayer; } - currentLayer[stringId >> (sizeof(uintptr_t) - 4)] = NULL; + free(currentLayer[stringId >> (BITS(uintptr_t) - 4)]); + currentLayer[stringId >> (BITS(uintptr_t) - 4)] = NULL; } diff --git a/src/kernel/stringmap/stringmapSyscalls.c b/src/kernel/stringmap/stringmapSyscalls.c index 9ee8c6f..33e3d4e 100644 --- a/src/kernel/stringmap/stringmapSyscalls.c +++ b/src/kernel/stringmap/stringmapSyscalls.c @@ -29,3 +29,8 @@ memcpy(string, buffer, size + 1); unmapPage(buffer); } + +void handleDiscardStringSyscall(Syscall *call) { + uintptr_t stringId = call->parameters[0]; + discardString(stringId); +} diff --git a/src/kernel/syscalls/syscall.c b/src/kernel/syscalls/syscall.c index f1cb269..abe9808 100644 --- a/src/kernel/syscalls/syscall.c +++ b/src/kernel/syscalls/syscall.c @@ -57,7 +57,7 @@ handleFireEventSyscall, handleSubscribeEventSyscall; extern uintptr_t handleSubscribeInterruptSyscall; extern uintptr_t handleInsertStringSyscall, handleReadStringLengthSyscall, - handleReadStringSyscall; + handleReadStringSyscall, handleDiscardStringSyscall; void (*syscallHandlers[])(Syscall *) = { 0, @@ -77,6 +77,7 @@ (void *)&handleInsertStringSyscall, (void *)&handleReadStringLengthSyscall, (void *)&handleReadStringSyscall, + (void *)&handleDiscardStringSyscall, }; void processSyscall(Syscall *call) { diff --git a/src/kernel/util/strings.c b/src/kernel/util/strings.c index b023bcb..324a58e 100644 --- a/src/kernel/util/strings.c +++ b/src/kernel/util/strings.c @@ -13,6 +13,9 @@ } uint32_t strlen(char *string) { + if (!string) { + return 0; + } uint32_t size = 0; while (*string) { string++; diff --git a/src/userland/hlib/include/syscalls.h b/src/userland/hlib/include/syscalls.h index 07251c3..dc85e4b 100644 --- a/src/userland/hlib/include/syscalls.h +++ b/src/userland/hlib/include/syscalls.h @@ -22,6 +22,7 @@ SYS_INSERT_STRING = 14, SYS_GET_STRING_LENGTH = 15, SYS_READ_STRING = 16, + SYS_DISCARD_STRING = 17, } SyscallIds; #endif diff --git a/src/include/hlib.h b/src/include/hlib.h index 1e94acb..d36b727 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -32,5 +32,6 @@ extern uintptr_t insertString(char *string, uintptr_t size); extern uintptr_t getStringLength(uintptr_t stringId); extern void readString(uintptr_t stringId, void *buffer); +extern void discardString(uintptr_t stringId); #endif diff --git a/src/kernel/stringmap/stringmap.c b/src/kernel/stringmap/stringmap.c index 6e0cea1..44a287f 100644 --- a/src/kernel/stringmap/stringmap.c +++ b/src/kernel/stringmap/stringmap.c @@ -47,13 +47,13 @@ void discardString(uintptr_t stringId) { MapLayer currentLayer = rootLayer; - for (uint32_t startBit = 0; startBit < sizeof(uintptr_t) - 4; - startBit += 4) { + for (uint32_t startBit = 0; startBit < BITS(uintptr_t) - 4; startBit += 4) { void *nextLayer = currentLayer[(stringId >> startBit) & 0xF]; if (!nextLayer) { return; } currentLayer = nextLayer; } - currentLayer[stringId >> (sizeof(uintptr_t) - 4)] = NULL; + free(currentLayer[stringId >> (BITS(uintptr_t) - 4)]); + currentLayer[stringId >> (BITS(uintptr_t) - 4)] = NULL; } diff --git a/src/kernel/stringmap/stringmapSyscalls.c b/src/kernel/stringmap/stringmapSyscalls.c index 9ee8c6f..33e3d4e 100644 --- a/src/kernel/stringmap/stringmapSyscalls.c +++ b/src/kernel/stringmap/stringmapSyscalls.c @@ -29,3 +29,8 @@ memcpy(string, buffer, size + 1); unmapPage(buffer); } + +void handleDiscardStringSyscall(Syscall *call) { + uintptr_t stringId = call->parameters[0]; + discardString(stringId); +} diff --git a/src/kernel/syscalls/syscall.c b/src/kernel/syscalls/syscall.c index f1cb269..abe9808 100644 --- a/src/kernel/syscalls/syscall.c +++ b/src/kernel/syscalls/syscall.c @@ -57,7 +57,7 @@ handleFireEventSyscall, handleSubscribeEventSyscall; extern uintptr_t handleSubscribeInterruptSyscall; extern uintptr_t handleInsertStringSyscall, handleReadStringLengthSyscall, - handleReadStringSyscall; + handleReadStringSyscall, handleDiscardStringSyscall; void (*syscallHandlers[])(Syscall *) = { 0, @@ -77,6 +77,7 @@ (void *)&handleInsertStringSyscall, (void *)&handleReadStringLengthSyscall, (void *)&handleReadStringSyscall, + (void *)&handleDiscardStringSyscall, }; void processSyscall(Syscall *call) { diff --git a/src/kernel/util/strings.c b/src/kernel/util/strings.c index b023bcb..324a58e 100644 --- a/src/kernel/util/strings.c +++ b/src/kernel/util/strings.c @@ -13,6 +13,9 @@ } uint32_t strlen(char *string) { + if (!string) { + return 0; + } uint32_t size = 0; while (*string) { string++; diff --git a/src/userland/hlib/include/syscalls.h b/src/userland/hlib/include/syscalls.h index 07251c3..dc85e4b 100644 --- a/src/userland/hlib/include/syscalls.h +++ b/src/userland/hlib/include/syscalls.h @@ -22,6 +22,7 @@ SYS_INSERT_STRING = 14, SYS_GET_STRING_LENGTH = 15, SYS_READ_STRING = 16, + SYS_DISCARD_STRING = 17, } SyscallIds; #endif diff --git a/src/userland/hlib/main.c b/src/userland/hlib/main.c index 6734cdc..f11ba62 100644 --- a/src/userland/hlib/main.c +++ b/src/userland/hlib/main.c @@ -94,3 +94,7 @@ void readString(uintptr_t stringId, void *buffer) { syscall(SYS_READ_STRING, stringId, U32(buffer), 0, 0); } + +void discardString(uintptr_t stringId) { + syscall(SYS_DISCARD_STRING, stringId, 0, 0, 0); +} diff --git a/src/include/hlib.h b/src/include/hlib.h index 1e94acb..d36b727 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -32,5 +32,6 @@ extern uintptr_t insertString(char *string, uintptr_t size); extern uintptr_t getStringLength(uintptr_t stringId); extern void readString(uintptr_t stringId, void *buffer); +extern void discardString(uintptr_t stringId); #endif diff --git a/src/kernel/stringmap/stringmap.c b/src/kernel/stringmap/stringmap.c index 6e0cea1..44a287f 100644 --- a/src/kernel/stringmap/stringmap.c +++ b/src/kernel/stringmap/stringmap.c @@ -47,13 +47,13 @@ void discardString(uintptr_t stringId) { MapLayer currentLayer = rootLayer; - for (uint32_t startBit = 0; startBit < sizeof(uintptr_t) - 4; - startBit += 4) { + for (uint32_t startBit = 0; startBit < BITS(uintptr_t) - 4; startBit += 4) { void *nextLayer = currentLayer[(stringId >> startBit) & 0xF]; if (!nextLayer) { return; } currentLayer = nextLayer; } - currentLayer[stringId >> (sizeof(uintptr_t) - 4)] = NULL; + free(currentLayer[stringId >> (BITS(uintptr_t) - 4)]); + currentLayer[stringId >> (BITS(uintptr_t) - 4)] = NULL; } diff --git a/src/kernel/stringmap/stringmapSyscalls.c b/src/kernel/stringmap/stringmapSyscalls.c index 9ee8c6f..33e3d4e 100644 --- a/src/kernel/stringmap/stringmapSyscalls.c +++ b/src/kernel/stringmap/stringmapSyscalls.c @@ -29,3 +29,8 @@ memcpy(string, buffer, size + 1); unmapPage(buffer); } + +void handleDiscardStringSyscall(Syscall *call) { + uintptr_t stringId = call->parameters[0]; + discardString(stringId); +} diff --git a/src/kernel/syscalls/syscall.c b/src/kernel/syscalls/syscall.c index f1cb269..abe9808 100644 --- a/src/kernel/syscalls/syscall.c +++ b/src/kernel/syscalls/syscall.c @@ -57,7 +57,7 @@ handleFireEventSyscall, handleSubscribeEventSyscall; extern uintptr_t handleSubscribeInterruptSyscall; extern uintptr_t handleInsertStringSyscall, handleReadStringLengthSyscall, - handleReadStringSyscall; + handleReadStringSyscall, handleDiscardStringSyscall; void (*syscallHandlers[])(Syscall *) = { 0, @@ -77,6 +77,7 @@ (void *)&handleInsertStringSyscall, (void *)&handleReadStringLengthSyscall, (void *)&handleReadStringSyscall, + (void *)&handleDiscardStringSyscall, }; void processSyscall(Syscall *call) { diff --git a/src/kernel/util/strings.c b/src/kernel/util/strings.c index b023bcb..324a58e 100644 --- a/src/kernel/util/strings.c +++ b/src/kernel/util/strings.c @@ -13,6 +13,9 @@ } uint32_t strlen(char *string) { + if (!string) { + return 0; + } uint32_t size = 0; while (*string) { string++; diff --git a/src/userland/hlib/include/syscalls.h b/src/userland/hlib/include/syscalls.h index 07251c3..dc85e4b 100644 --- a/src/userland/hlib/include/syscalls.h +++ b/src/userland/hlib/include/syscalls.h @@ -22,6 +22,7 @@ SYS_INSERT_STRING = 14, SYS_GET_STRING_LENGTH = 15, SYS_READ_STRING = 16, + SYS_DISCARD_STRING = 17, } SyscallIds; #endif diff --git a/src/userland/hlib/main.c b/src/userland/hlib/main.c index 6734cdc..f11ba62 100644 --- a/src/userland/hlib/main.c +++ b/src/userland/hlib/main.c @@ -94,3 +94,7 @@ void readString(uintptr_t stringId, void *buffer) { syscall(SYS_READ_STRING, stringId, U32(buffer), 0, 0); } + +void discardString(uintptr_t stringId) { + syscall(SYS_DISCARD_STRING, stringId, 0, 0, 0); +} diff --git a/src/userland/loader/main.c b/src/userland/loader/main.c index 54e9a72..cdef5a7 100644 --- a/src/userland/loader/main.c +++ b/src/userland/loader/main.c @@ -19,5 +19,9 @@ log(lengthString); readString(id, readBuffer); log(readBuffer); + discardString(id); + length = getStringLength(id); + lengthString[0] = '0' + length; + log(lengthString); return 0; }