]> git.kernelconcepts.de Git - karo-tx-uboot.git/commitdiff
sandbox: Improve/augment memory allocation functions
authorSimon Glass <sjg@chromium.org>
Sun, 10 Nov 2013 17:26:57 +0000 (10:26 -0700)
committerSimon Glass <sjg@chromium.org>
Thu, 9 Jan 2014 00:24:19 +0000 (17:24 -0700)
Implement realloc() and free() for sandbox, by adding a header to each
block which contains the block size.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Che-Liang Chiou <clchiou@chromium.org>
Reviewed-by: Hung-ying Tyan <tyanh@chromium.org>
arch/sandbox/cpu/os.c
include/os.h

index 26f44cb597ef14cd8f05376e0b96fd2ab2522ae1..88dd371760b7e20094cc32e57917f3a62633b424 100644 (file)
 
 /* Operating System Interface */
 
+struct os_mem_hdr {
+       size_t length;          /* number of bytes in the block */
+};
+
 ssize_t os_read(int fd, void *buf, size_t count)
 {
        return read(fd, buf, count);
@@ -128,8 +132,45 @@ void os_tty_raw(int fd)
 
 void *os_malloc(size_t length)
 {
-       return mmap(NULL, length, PROT_READ | PROT_WRITE,
-                       MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+       struct os_mem_hdr *hdr;
+
+       hdr = mmap(NULL, length + sizeof(*hdr), PROT_READ | PROT_WRITE,
+                  MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+       if (hdr == MAP_FAILED)
+               return NULL;
+       hdr->length = length;
+
+       return hdr + 1;
+}
+
+void *os_free(void *ptr)
+{
+       struct os_mem_hdr *hdr = ptr;
+
+       hdr--;
+       if (ptr)
+               munmap(hdr, hdr->length + sizeof(*hdr));
+}
+
+void *os_realloc(void *ptr, size_t length)
+{
+       struct os_mem_hdr *hdr = ptr;
+       void *buf = NULL;
+
+       hdr--;
+       if (length != 0) {
+               buf = os_malloc(length);
+               if (!buf)
+                       return buf;
+               if (ptr) {
+                       if (length > hdr->length)
+                               length = hdr->length;
+                       memcpy(buf, ptr, length);
+               }
+       }
+       os_free(ptr);
+
+       return buf;
 }
 
 void os_usleep(unsigned long usec)
index 950433daa32dbabce603e3a4941d142162e5506b..1575a969222f6ad2146b1b6ce2dc727c36663e04 100644 (file)
@@ -106,6 +106,35 @@ void os_tty_raw(int fd);
  */
 void *os_malloc(size_t length);
 
+/**
+ * Free memory previous allocated with os_malloc()/os_realloc()
+ *
+ * This returns the memory to the OS.
+ *
+ * \param ptr          Pointer to memory block to free
+ */
+void *os_free(void *ptr);
+
+/**
+ * Reallocate previously-allocated memory to increase/decrease space
+ *
+ * This works in a similar way to the C library realloc() function. If
+ * length is 0, then ptr is freed. Otherwise the space used by ptr is
+ * expanded or reduced depending on whether length is larger or smaller
+ * than before.
+ *
+ * If ptr is NULL, then this is similar to calling os_malloc().
+ *
+ * This function may need to move the memory block to make room for any
+ * extra space, in which case the new pointer is returned.
+ *
+ * \param ptr          Pointer to memory block to reallocate
+ * \param length       New length for memory block
+ * \return pointer to new memory block, or NULL on failure or if length
+ *     is 0.
+ */
+void *os_realloc(void *ptr, size_t length);
+
 /**
  * Access to the usleep function of the os
  *