瀏覽代碼

Add zero allocate function, use auto and constexpr

Kajetan Johannes Hammerle 1 月之前
父節點
當前提交
5632e2b3b9
共有 5 個文件被更改,包括 48 次插入23 次删除
  1. 6 4
      include/core/Utility.h
  2. 3 3
      src/BitArray.c
  3. 13 13
      src/ReadLine.c
  4. 15 3
      src/Utility.c
  5. 11 0
      test/modules/UtilityTests.c

+ 6 - 4
include/core/Utility.h

@@ -29,14 +29,17 @@ void setOutOfMemoryHandler(OutOfMemoryHandler h, void* data);
 
 #ifdef CHECK_MEMORY
 void* coreDebugAllocate(const char* file, int line, size_t n);
+void* coreDebugZeroAllocate(const char* file, int line, size_t n);
 void* coreDebugReallocate(const char* file, int line, void* p, size_t n);
 void coreFreeDebug(const char* file, int line, void* p);
 void printMemoryReport(void);
 #define coreAllocate(n) coreDebugAllocate(__FILE__, __LINE__, n)
+#define coreZeroAllocate(n) coreDebugZeroAllocate(__FILE__, __LINE__, n)
 #define coreReallocate(p, n) coreDebugReallocate(__FILE__, __LINE__, p, n)
 #define coreFree(p) coreFreeDebug(__FILE__, __LINE__, p)
 #else
 void* coreAllocate(size_t n);
+void* coreZeroAllocate(size_t n);
 void* coreReallocate(void* p, size_t n);
 void coreFree(void* p);
 #define printMemoryReport()
@@ -45,12 +48,11 @@ void coreFree(void* p);
 bool sleepNanos(i64 nanos);
 i64 getNanos(void);
 
-// TODO: replace typeof with auto when available
 #define swap(a, b)                                                             \
     do {                                                                       \
-        typeof(a) aPointer = (a);                                              \
-        typeof(b) bPointer = (b);                                              \
-        typeof(*aPointer) tmp = *aPointer;                                     \
+        auto aPointer = (a);                                                   \
+        auto bPointer = (b);                                                   \
+        auto tmp = *aPointer;                                                  \
         *aPointer = *bPointer;                                                 \
         *bPointer = tmp;                                                       \
     } while(0)

+ 3 - 3
src/BitArray.c

@@ -7,13 +7,13 @@
 #include "core/ToString.h"
 #include "core/Utility.h"
 
+static constexpr size_t U64_BITS = 64;
+static constexpr size_t DIVIDE_BITS = 6;
+
 static u64 roundUpDivide(u64 a, u64 b) {
     return a / b + ((a % b) != 0);
 }
 
-#define U64_BITS 64
-#define DIVIDE_BITS 6
-
 static u64 readBits(const u64* data, size_t index, u64 bits) {
     u64 dataIndexA = (index * bits) >> DIVIDE_BITS;
     u64 dataIndexB = ((index * bits) + (bits - 1lu)) >> DIVIDE_BITS;

+ 13 - 13
src/ReadLine.c

@@ -14,12 +14,12 @@
 #include "core/Queue.h"
 #include "core/Thread.h"
 
-#define HISTORY_LENGTH 10
-#define CONSOLE_BUFFER_SIZE 256
+static constexpr size_t HISTORY_LENGTH = 10;
+static constexpr size_t CONSOLE_BUFFER_SIZE = 256;
 
 typedef struct ConsoleLine {
     char data[CONSOLE_BUFFER_SIZE];
-    int length;
+    size_t length;
 } ConsoleLine;
 
 QUEUE(ConsoleLine, CL)
@@ -31,14 +31,14 @@ static thrd_t readThread = {0};
 static struct termios original;
 static QueueCL buffer = {0};
 static ConsoleLine currentBuffer = {0};
-static int move = 0;
-static int cursorMove = 0;
+static size_t move = 0;
+static size_t cursorMove = 0;
 static mtx_t bufferMutex = {0};
 
 static ConsoleLine history[HISTORY_LENGTH];
-static int historyOffset = 0;
-static int historyIndex = 0;
-static int historyLength = 0;
+static size_t historyOffset = 0;
+static size_t historyIndex = 0;
+static size_t historyLength = 0;
 
 static void getAttributes(struct termios* t) {
     if(tcgetattr(STDIN_FILENO, t)) {
@@ -62,7 +62,7 @@ static void addChar(char c) {
     if(currentBuffer.length >= CONSOLE_BUFFER_SIZE - 1) {
         return;
     }
-    for(int i = 0; i < move; i++) {
+    for(size_t i = 0; i < move; i++) {
         currentBuffer.data[currentBuffer.length - i] =
             currentBuffer.data[currentBuffer.length - i - 1];
     }
@@ -79,7 +79,7 @@ static void refreshLine(const char* prefix) {
     print(prefix);
     print(currentBuffer.data);
     if(cursorMove > 0) {
-        printf("\33[%dD", cursorMove);
+        printf("\33[%zuD", cursorMove);
     }
     fflush(stdout);
     print("\33[2K\r");
@@ -127,9 +127,9 @@ static char isUTF8Part(char c) {
 }
 
 static bool removeChar() {
-    int pos = currentBuffer.length - move;
+    size_t pos = currentBuffer.length - move;
     if(pos > 0) {
-        int l = 1;
+        size_t l = 1;
         while(pos - l >= 0) {
             if(!isUTF8Part(currentBuffer.data[pos - l])) {
                 break;
@@ -137,7 +137,7 @@ static bool removeChar() {
             l++;
         }
         currentBuffer.length -= l;
-        for(int i = pos - l; i <= currentBuffer.length; i++) {
+        for(size_t i = pos - l; i <= currentBuffer.length; i++) {
             currentBuffer.data[i] = currentBuffer.data[i + l];
         }
     }

+ 15 - 3
src/Utility.c

@@ -143,6 +143,12 @@ void* coreDebugAllocate(const char* file, int line, size_t n) {
     return (char*)p + sizeof(MemoryInfo);
 }
 
+void* coreDebugZeroAllocate(const char* file, int line, size_t n) {
+    void* p = coreDebugAllocate(file, line, n);
+    memset(p, 0, n);
+    return p;
+}
+
 void* coreDebugReallocate(const char* file, int line, void* p, size_t n) {
     if(n > 0) {
         n += sizeof(MemoryInfo) + sizeof(CANARY);
@@ -191,15 +197,21 @@ void printMemoryReport() {
 
 #else
 
-void* Allocate(size_t n) {
+void* coreAllocate(size_t n) {
     return RealAllocate(n);
 }
 
-void* Reallocate(void* p, size_t n) {
+void* coreZeroAllocate(size_t n) {
+    void* p = coreAllocate(n);
+    memset(p, 0, n);
+    return p;
+}
+
+void* coreReallocate(void* p, size_t n) {
     return RealReallocate(p, n);
 }
 
-void Free(void* p) {
+void coreFree(void* p) {
     RealFree(p);
 }
 

+ 11 - 0
test/modules/UtilityTests.c

@@ -34,6 +34,16 @@ static void testMemoryInfoList() {
     coreFree(nullptr);
 }
 
+static void testZeroAllocate() {
+    constexpr size_t n = 1024 * 1024;
+    void* a = coreAllocate(n);
+    memset(a, 0, n);
+    void* b = coreZeroAllocate(n);
+    TEST_TRUE(memcmp(a, b, n) == 0);
+    coreFree(a);
+    coreFree(b);
+}
+
 static void testInterpolate() {
     TEST_FLOAT(7.5f, interpolate(5.0f, 10.0f, 0.5f), eps);
     TEST_FLOAT(-2.0, interpolate(-10.0f, 10.0f, 0.4f), eps);
@@ -105,6 +115,7 @@ void testUtility(bool light) {
     testPopCount();
     testZeroRellocate();
     testMemoryInfoList();
+    testZeroAllocate();
     testInterpolate();
     testRadianToDegree();
     testDegreeToRadian();