]> granicus.if.org Git - esp-idf/commitdiff
esp32: modify realloc behaves just like malloc when pass a null pointer for ptr.
authorliuhan <liuhan@espressif.com>
Thu, 8 Sep 2016 02:50:52 +0000 (10:50 +0800)
committerAngus Gratton <angus@espressif.com>
Fri, 9 Sep 2016 05:06:56 +0000 (15:06 +1000)
components/esp32/syscalls.c

index 636e32670edb2e0bcae90a21e605cfabc6903e9f..1aff0167aa9ca5c7f393ac28100bbab726d69714 100644 (file)
@@ -44,10 +44,17 @@ void _free_r(struct _reent *r, void* ptr) {
     return vPortFree(ptr);
 }
 
-// TODO: improve realloc to grow buffer in place if possible
 void* _realloc_r(struct _reent *r, void* ptr, size_t size) {
-       void* new_chunk = pvPortMalloc(size);
-       if (new_chunk) {
+       void* new_chunk;
+       if (size == 0) {
+               if (ptr) {
+                       vPortFree(ptr);
+               }
+               return NULL;
+       }
+
+       new_chunk = pvPortMalloc(size);
+       if (new_chunk && ptr) {
                memcpy(new_chunk, ptr, size);
                vPortFree(ptr);
        }