[libc-commits] [PATCH] D157653: [libc][WIP] move realloc into alloc_checker

Michael Jones via Phabricator via libc-commits libc-commits at lists.llvm.org
Thu Aug 10 14:24:59 PDT 2023


michaelrj updated this revision to Diff 549170.
michaelrj added a comment.

cleanup some experimental code


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157653/new/

https://reviews.llvm.org/D157653

Files:
  libc/src/__support/CPP/new.h
  libc/src/__support/File/file.cpp
  libc/src/__support/char_vector.h


Index: libc/src/__support/char_vector.h
===================================================================
--- libc/src/__support/char_vector.h
+++ libc/src/__support/char_vector.h
@@ -9,6 +9,7 @@
 #ifndef LLVM_LIBC_SRC_SUPPORT_CHARVECTOR_H
 #define LLVM_LIBC_SRC_SUPPORT_CHARVECTOR_H
 
+#include "src/__support/CPP/new.h"
 #include "src/__support/common.h"
 
 #include <stddef.h>
@@ -33,7 +34,7 @@
   CharVector() = default;
   LIBC_INLINE ~CharVector() {
     if (cur_str != local_buffer)
-      free(cur_str);
+      delete (cur_str);
   }
 
   // append returns true on success and false on allocation failure.
@@ -42,11 +43,12 @@
     if (index >= cur_buff_size - 2) {
       // If the new character would cause the string to be longer than the
       // buffer's size, attempt to allocate a new buffer.
+      AllocChecker ac;
       cur_buff_size = cur_buff_size * 2;
       if (cur_str == local_buffer) {
         char *new_str;
-        new_str = reinterpret_cast<char *>(malloc(cur_buff_size));
-        if (new_str == NULL) {
+        new_str = new (ac) char[cur_buff_size];
+        if (!ac) {
           return false;
         }
         // TODO: replace with inline memcpy
@@ -54,8 +56,9 @@
           new_str[i] = cur_str[i];
         cur_str = new_str;
       } else {
-        cur_str = reinterpret_cast<char *>(realloc(cur_str, cur_buff_size));
-        if (cur_str == NULL) {
+        cur_str = reinterpret_cast<char *>(
+            AllocChecker::realloc(cur_str, cur_buff_size, ac));
+        if (!ac) {
           return false;
         }
       }
Index: libc/src/__support/File/file.cpp
===================================================================
--- libc/src/__support/File/file.cpp
+++ libc/src/__support/File/file.cpp
@@ -355,14 +355,12 @@
   if (buffer == nullptr && size != 0 && buffer_mode != _IONBF) {
     // We exclude the case of buffer_mode == _IONBF in this branch
     // because we don't need to allocate buffer in such a case.
+    AllocChecker ac;
     if (own_buf) {
-      // This is one of the places where use a C allocation functon
-      // as C++ does not have an equivalent of realloc.
-      buf = reinterpret_cast<uint8_t *>(realloc(buf, size));
-      if (buf == nullptr)
+      buf = reinterpret_cast<uint8_t *>(AllocChecker::realloc(buf, size, ac));
+      if (!ac)
         return ENOMEM;
     } else {
-      AllocChecker ac;
       buf = new (ac) uint8_t[size];
       if (!ac)
         return ENOMEM;
Index: libc/src/__support/CPP/new.h
===================================================================
--- libc/src/__support/CPP/new.h
+++ libc/src/__support/CPP/new.h
@@ -9,6 +9,10 @@
 #ifndef LLVM_LIBC_SRC_SUPPORT_CPP_NEW_H
 #define LLVM_LIBC_SRC_SUPPORT_CPP_NEW_H
 
+#ifdef LIBC_COPT_DISABLE_NEW
+#warning "new included when LIBC_COPT_DISABLE_NEW is set."
+#endif
+
 #include "src/__support/common.h"
 
 #include <stddef.h> // For size_t
@@ -50,6 +54,12 @@
     ac = (mem != nullptr);
     return mem;
   }
+
+  LIBC_INLINE static void *realloc(void *ptr, size_t s, AllocChecker &ac) {
+    void *mem = ::realloc(ptr, s);
+    ac = (mem != nullptr);
+    return mem;
+  }
 };
 
 } // namespace __llvm_libc


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D157653.549170.patch
Type: text/x-patch
Size: 3177 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20230810/c50191fa/attachment.bin>


More information about the libc-commits mailing list