summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/array.c2
-rw-r--r--src/lib.c13
-rw-r--r--src/str.c51
3 files changed, 22 insertions, 44 deletions
diff --git a/src/array.c b/src/array.c
index ffdc36b..c843ff6 100644
--- a/src/array.c
+++ b/src/array.c
@@ -1,7 +1,7 @@
#include "../include/al/lib.h"
#include "../include/al/array_sort.h"
-// based on: https://github.com/lifthrasiir/angolmois/blob/master/angolmois.c#L79
+// https://github.com/lifthrasiir/angolmois/blob/master/angolmois.c#L79
#define array(type) \
struct { \
diff --git a/src/lib.c b/src/lib.c
index 2df40d3..64a60a3 100644
--- a/src/lib.c
+++ b/src/lib.c
@@ -1,4 +1,4 @@
-// Testing allocation tracking stuff. (Very scuffed)
+// Testing allocation tracking stuff. (Very scuffed, not thread-safe!)
#include "../include/al/lib.h"
@@ -20,7 +20,7 @@ struct alloc_t {
};
// Use stdlib allocation for the allocation tracking array.
-// This avoids a circular loop and also keeps allocations
+// This avoids a circular dependency and also keeps allocations
// from the memory tracking code out of the stats.
#include "../include/al/types.h"
#define AL_ARRAY_MALLOC malloc
@@ -29,9 +29,9 @@ struct alloc_t {
#include "array.c"
static array(struct alloc_t) allocations;
-static u32 total_allocated = 0;
static u32 currently_allocated = 0;
static u32 peak_allocated = 0;
+static u32 total_allocated = 0;
#endif
void al_malloc_init(void)
@@ -41,6 +41,13 @@ void al_malloc_init(void)
#endif
}
+void al_malloc_close(void)
+{
+#if MEMORY_TRACKING
+ al_array_free(allocations);
+#endif
+}
+
void *al_malloc(size_t n)
{
void *ptr = _al_malloc(n);
diff --git a/src/str.c b/src/str.c
index a3c0946..2bb7fa9 100644
--- a/src/str.c
+++ b/src/str.c
@@ -60,54 +60,25 @@ s64 al_str_to_long(str *s, s32 base)
return ret;
}
-bool al_str_tok(str *s, char sep, str *tok)
+bool al_str_get_line(str *buffer, char sep, str *line)
{
- if (tok->alloc == s->len + 1) {
- return false;
- }
- u32 r = s->len - tok->alloc;
-#ifndef __cplusplus
- s32 i = al_str_find(al_str_w(s->data, tok->alloc, r), sep);
-#else
- str w = al_str_ww(s->data, tok->alloc, r);
- s32 i = al_str_find(&w, sep);
-#endif
- if (i == -1) i = r;
- tok->data = s->data + tok->alloc;
- tok->len = (u32)i;
- tok->alloc += tok->len + 1;
- return true;
-}
-
-bool al_str_get_line(str *buffer, str *line)
-{
- s32 bytes_processed = 0;
+ u32 bytes = 0;
if (line->data == NULL) {
*line = *buffer;
- } else {
- // Move to the end of the current line.
- line->data += line->len;
-
- bytes_processed = (s32)((line->data - buffer->data) + 1);
-
- // If line->ptr is at the last character, we are done.
- // Also, if bytes_processed <= 0 buffer is empty or invalid, so return.
- if (bytes_processed >= (s32)buffer->len || bytes_processed <= 0) {
- return false;
- }
-
- // Else, move line->ptr to the first character on the next line.
- line->data++;
+ } else if ((bytes = (u32)((line->data += line->len + 1) - buffer->data)) >= buffer->len) {
+ // Move to the start of the assumed next line. If `bytes >= buffer->len`,
+ // there is no next line.
+ return false;
}
- // Find the next newline character.
- char *nl = (char *)memchr(line->data, '\n', buffer->len - bytes_processed);
+ // Remaining bytes.
+ bytes = buffer->len - bytes;
- // If there is no newline character move to the end of the buffer.
- if (!nl) nl = buffer->data + buffer->len;
+ char *nl = (char *)al_memchr(line->data, sep, bytes);
- line->len = (u32)(nl - line->data);
+ // If `!nl`, move to the end of the buffer.
+ line->len = !nl ? bytes : (u32)(nl - line->data);
return true;
}