Skip to contentSkip to conversation

tools/libxl: Correct libxl__zalloc() to take an unsigned number of bytes

PATCH In review Andrew Cooper 2 participants 3 messages Mar 27, 2014 – Apr 1, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 tools/libxl: Correct libxl__zalloc() to take an unsigned number of bytes

Convert 'int bytes' to 'size_t size' to mirror malloc(3) which it is imitating, and calloc(3) which it is actually using.

CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>
---
 tools/libxl/libxl_internal.c |    6 +++---
 tools/libxl/libxl_internal.h |    4 ++--
 2 files changed, 5 insertions(+), 5 deletions(-)
tools/libxl/libxl_internal.c +3 -3
index 1db48b6..6c105de 100644
@@ -78,10 +78,10 @@ void libxl__free_all(libxl__gc *gc)
     gc->alloc_maxsize = 0;
 }
 
-void *libxl__zalloc(libxl__gc *gc, int bytes)
+void *libxl__zalloc(libxl__gc *gc, size_t size)
 {
-    void *ptr = calloc(bytes, 1);
-    if (!ptr) libxl__alloc_failed(CTX, __func__, bytes, 1);
+    void *ptr = calloc(size, 1);
+    if (!ptr) libxl__alloc_failed(CTX, __func__, size, 1);
 
     libxl__ptr_add(gc, ptr);
     return ptr;
tools/libxl/libxl_internal.h +2 -2
index b3a200d..be325bb 100644
@@ -504,8 +504,8 @@ static inline int libxl__gc_is_real(const libxl__gc *gc)
 _hidden void libxl__ptr_add(libxl__gc *gc_opt, void *ptr /* may be NULL */) NN1;
 /* if this is the outermost libxl callframe then free all pointers in @gc */
 _hidden void libxl__free_all(libxl__gc *gc);
-/* allocate and zero @bytes. (similar to a gc'd malloc(3)+memzero()) */
-_hidden void *libxl__zalloc(libxl__gc *gc_opt, int bytes) NN1;
+/* allocate and zero @size. (similar to a gc'd malloc(3)+memzero()) */
+_hidden void *libxl__zalloc(libxl__gc *gc_opt, size_t size) NN1;
 /* allocate and zero memory for an array of @nmemb members of @size each.
  * (similar to a gc'd calloc(3)). */
 _hidden void *libxl__calloc(libxl__gc *gc_opt, size_t nmemb, size_t size) NN1;
Signature · 1 line
1.7.10.4
#1 lore raw

PATCH 2/2 tools/libxl: Introduce libxl__malloc()

For those large allocations which are going to be filled immediately, where redundantly clearing the memory would just be a waste of time.

CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>
---
 tools/libxl/libxl_internal.c |    9 +++++++++
 tools/libxl/libxl_internal.h |    2 ++
 2 files changed, 11 insertions(+)
tools/libxl/libxl_internal.c +9 -0
index 6c105de..6c94d3e 100644
@@ -78,6 +78,15 @@ void libxl__free_all(libxl__gc *gc)
     gc->alloc_maxsize = 0;
 }
 
+void *libxl__malloc(libxl__gc *gc, size_t size)
+{
+    void *ptr = malloc(size);
+    if (!ptr) libxl__alloc_failed(CTX, __func__, size, 1);
+
+    libxl__ptr_add(gc, ptr);
+    return ptr;
+}
+
 void *libxl__zalloc(libxl__gc *gc, size_t size)
 {
     void *ptr = calloc(size, 1);
tools/libxl/libxl_internal.h +2 -0
index be325bb..c2b73c4 100644
@@ -504,6 +504,8 @@ static inline int libxl__gc_is_real(const libxl__gc *gc)
 _hidden void libxl__ptr_add(libxl__gc *gc_opt, void *ptr /* may be NULL */) NN1;
 /* if this is the outermost libxl callframe then free all pointers in @gc */
 _hidden void libxl__free_all(libxl__gc *gc);
+/* allocate @size bytes. (a gc'd malloc(3)) */
+_hidden void *libxl__malloc(libxl__gc *gc_opt, size_t size) NN1;
 /* allocate and zero @size. (similar to a gc'd malloc(3)+memzero()) */
 _hidden void *libxl__zalloc(libxl__gc *gc_opt, size_t size) NN1;
 /* allocate and zero memory for an array of @nmemb members of @size each.
Signature · 1 line
1.7.10.4