]> git.ipfire.org Git - thirdparty/e2fsprogs.git/commitdiff
e2fsprogs: Add memory allocation and zero-out helpers
authorLukas Czerner <lczerner@redhat.com>
Wed, 18 May 2011 12:19:52 +0000 (14:19 +0200)
committerTheodore Ts'o <tytso@mit.edu>
Wed, 18 May 2011 15:41:02 +0000 (11:41 -0400)
Add functions ext2fs_get_memzero() which will malloc() the memory
using ext2fs_get_mem(), but it will zero the allocated memory afterwards
with memset().

Add function ext2fs_get_arrayzero() which will use calloc() for
allocating and zero-out the array.

Signed-off-by: Lukas Czerner <lczerner@redhat.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
lib/ext2fs/ext2fs.h

index d3eb31df3398856eaaa9e8ee15430ba30dddae87..888f4251e2165fafdc7d8be1730bf3cb3c1c3e22 100644 (file)
@@ -1404,13 +1404,39 @@ _INLINE_ errcode_t ext2fs_get_memalign(unsigned long size,
        return 0;
 }
 
+_INLINE_ errcode_t ext2fs_get_memzero(unsigned long size, void *ptr)
+{
+       void *pp;
+
+       pp = malloc(size);
+       if (!pp)
+               return EXT2_ET_NO_MEMORY;
+       memset(pp, 0, size);
+       memcpy(ptr, &pp, sizeof(pp));
+       return 0;
+}
+
 _INLINE_ errcode_t ext2fs_get_array(unsigned long count, unsigned long size, void *ptr)
 {
        if (count && (-1UL)/count<size)
-               return EXT2_ET_NO_MEMORY; //maybe define EXT2_ET_OVERFLOW ?
+               return EXT2_ET_NO_MEMORY;
        return ext2fs_get_mem(count*size, ptr);
 }
 
+_INLINE_ errcode_t ext2fs_get_arrayzero(unsigned long count,
+                                       unsigned long size, void *ptr)
+{
+       void *pp;
+
+       if (count && (-1UL)/count<size)
+               return EXT2_ET_NO_MEMORY;
+       pp = calloc(count, size);
+       if (!pp)
+               return EXT2_ET_NO_MEMORY;
+       memcpy(ptr, &pp, sizeof(pp));
+       return 0;
+}
+
 /*
  * Free memory
  */