ambrop7 14 лет назад
Родитель
Сommit
1ff4330bef
1 измененных файлов с 20 добавлено и 0 удалено
  1. 20 0
      misc/balloc.h

+ 20 - 0
misc/balloc.h

@@ -32,6 +32,7 @@
 #include <stdlib.h>
 
 #include <misc/debug.h>
+#include <misc/bsize.h>
 
 /**
  * Allocates memory.
@@ -51,6 +52,16 @@ static void * BAlloc (size_t bytes);
  */
 static void BFree (void *m);
 
+/**
+ * Allocates memory, with size given as a {@link bsize_t}.
+ * 
+ * @param bytes number of bytes to allocate. If the size is overflow,
+ *              this function will return NULL.
+ * @return a non-NULL pointer to the memory, or NULL on failure.
+ *         The memory allocated can be freed using {@link BFree}.
+ */
+static void * BAllocSize (bsize_t bytes);
+
 /**
  * Allocates memory for an array.
  * A check is first done to make sure the multiplication doesn't overflow;
@@ -91,6 +102,15 @@ void BFree (void *m)
     free(m);
 }
 
+void * BAllocSize (bsize_t bytes)
+{
+    if (bytes.is_overflow) {
+        return NULL;
+    }
+    
+    return BAlloc(bytes.value);
+}
+
 void * BAllocArray (size_t count, size_t bytes)
 {
     if (count == 0 || bytes == 0) {