Skip to content

Commit b8b020f

Browse files
authored
Update with internal functions rather than external interfaces.
1 parent 6cc55f1 commit b8b020f

File tree

3 files changed

+46
-62
lines changed

3 files changed

+46
-62
lines changed

libc/src/stdlib/CMakeLists.txt

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -65,40 +65,38 @@ add_entrypoint_object(
6565
libc.config.app_h
6666
)
6767

68-
# Environment variable functions only make sense in full build mode.
69-
if(LLVM_LIBC_FULL_BUILD)
70-
add_object_library(
71-
environ_internal
72-
SRCS
73-
environ_internal.cpp
74-
HDRS
75-
environ_internal.h
76-
DEPENDS
77-
libc.config.app_h
78-
libc.hdr.types.size_t
79-
libc.src.__support.CPP.string_view
80-
libc.src.__support.threads.mutex
81-
libc.src.stdlib.free
82-
libc.src.stdlib.malloc
83-
libc.src.string.memcpy
84-
)
68+
add_object_library(
69+
environ_internal
70+
SRCS
71+
environ_internal.cpp
72+
HDRS
73+
environ_internal.h
74+
DEPENDS
75+
libc.config.app_h
76+
libc.hdr.types.size_t
77+
libc.src.__support.CPP.string_view
78+
libc.src.__support.threads.mutex
79+
libc.hdr.func.free
80+
libc.hdr.func.malloc
81+
libc.hdr.func.realloc
82+
)
8583

86-
add_entrypoint_object(
87-
setenv
88-
SRCS
89-
setenv.cpp
90-
HDRS
91-
setenv.h
92-
DEPENDS
93-
.environ_internal
94-
libc.src.__support.CPP.string_view
95-
libc.src.__support.libc_errno
96-
libc.src.__support.threads.mutex
97-
libc.src.stdlib.malloc
98-
libc.src.string.memcpy
99-
libc.src.string.strlen
100-
)
101-
endif()
84+
add_entrypoint_object(
85+
setenv
86+
SRCS
87+
setenv.cpp
88+
HDRS
89+
setenv.h
90+
DEPENDS
91+
.environ_internal
92+
libc.src.__support.CPP.string_view
93+
libc.src.__support.libc_errno
94+
libc.src.__support.threads.mutex
95+
libc.hdr.func.malloc
96+
libc.hdr.func.free
97+
libc.src.string.memory_utils.inline_memcpy
98+
libc.src.string.string_utils
99+
)
102100

103101
add_entrypoint_object(
104102
strfromf

libc/src/stdlib/environ_internal.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,11 @@
88

99
#include "environ_internal.h"
1010
#include "config/app.h"
11+
#include "hdr/func/free.h"
12+
#include "hdr/func/malloc.h"
13+
#include "hdr/func/realloc.h"
1114
#include "src/__support/CPP/string_view.h"
1215
#include "src/__support/macros/config.h"
13-
#include "src/string/memcpy.h"
14-
15-
// We use extern "C" declarations for malloc/free/realloc instead of including
16-
// src/stdlib/malloc.h, src/stdlib/free.h, and src/stdlib/realloc.h. This allows
17-
// the implementation to work with different allocator implementations,
18-
// particularly in integration tests which provide a simple bump allocator. The
19-
// extern "C" linkage ensures we use whatever allocator is linked with the test
20-
// or application.
21-
extern "C" void *malloc(size_t);
22-
extern "C" void free(void *);
23-
extern "C" void *realloc(void *, size_t);
2416

2517
namespace LIBC_NAMESPACE_DECL {
2618
namespace internal {

libc/src/stdlib/setenv.cpp

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,14 @@
88

99
#include "src/stdlib/setenv.h"
1010
#include "environ_internal.h"
11+
#include "hdr/func/free.h"
12+
#include "hdr/func/malloc.h"
1113
#include "src/__support/CPP/string_view.h"
1214
#include "src/__support/common.h"
1315
#include "src/__support/libc_errno.h"
1416
#include "src/__support/macros/config.h"
15-
#include "src/string/memcpy.h"
16-
#include "src/string/strlen.h"
17-
18-
// We use extern "C" declarations for malloc/free instead of including
19-
// src/stdlib/malloc.h and src/stdlib/free.h. This allows the implementation
20-
// to work with different allocator implementations, particularly in integration
21-
// tests which provide a simple bump allocator. The extern "C" linkage ensures
22-
// we use whatever allocator is linked with the test or application.
23-
extern "C" void *malloc(size_t);
24-
extern "C" void free(void *);
17+
#include "src/string/memory_utils/inline_memcpy.h"
18+
#include "src/string/string_utils.h"
2519

2620
namespace LIBC_NAMESPACE_DECL {
2721

@@ -64,8 +58,8 @@ LLVM_LIBC_FUNCTION(int, setenv,
6458

6559
// Need to replace the value
6660
// Calculate size for "name=value" string
67-
size_t name_len = LIBC_NAMESPACE::strlen(name);
68-
size_t value_len = LIBC_NAMESPACE::strlen(value);
61+
size_t name_len = LIBC_NAMESPACE::internal::string_length(name);
62+
size_t value_len = LIBC_NAMESPACE::internal::string_length(value);
6963
size_t total_len =
7064
name_len + 1 + value_len + 1; // name + '=' + value + '\0'
7165

@@ -77,9 +71,9 @@ LLVM_LIBC_FUNCTION(int, setenv,
7771
}
7872

7973
// Build "name=value" string
80-
LIBC_NAMESPACE::memcpy(new_string, name, name_len);
74+
LIBC_NAMESPACE::inline_memcpy(new_string, name, name_len);
8175
new_string[name_len] = '=';
82-
LIBC_NAMESPACE::memcpy(new_string + name_len + 1, value, value_len);
76+
LIBC_NAMESPACE::inline_memcpy(new_string + name_len + 1, value, value_len);
8377
new_string[name_len + 1 + value_len] = '\0';
8478

8579
// Replace in environ array
@@ -107,8 +101,8 @@ LLVM_LIBC_FUNCTION(int, setenv,
107101
}
108102

109103
// Calculate size for "name=value" string
110-
size_t name_len = LIBC_NAMESPACE::strlen(name);
111-
size_t value_len = LIBC_NAMESPACE::strlen(value);
104+
size_t name_len = LIBC_NAMESPACE::internal::string_length(name);
105+
size_t value_len = LIBC_NAMESPACE::internal::string_length(value);
112106
size_t total_len = name_len + 1 + value_len + 1; // name + '=' + value + '\0'
113107

114108
char *new_string = reinterpret_cast<char *>(malloc(total_len));
@@ -119,9 +113,9 @@ LLVM_LIBC_FUNCTION(int, setenv,
119113
}
120114

121115
// Build "name=value" string
122-
LIBC_NAMESPACE::memcpy(new_string, name, name_len);
116+
LIBC_NAMESPACE::inline_memcpy(new_string, name, name_len);
123117
new_string[name_len] = '=';
124-
LIBC_NAMESPACE::memcpy(new_string + name_len + 1, value, value_len);
118+
LIBC_NAMESPACE::inline_memcpy(new_string + name_len + 1, value, value_len);
125119
new_string[name_len + 1 + value_len] = '\0';
126120

127121
// Add to environ array

0 commit comments

Comments
 (0)