-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core: clean up memory management for headers (#342)
Description: this PR completes memory management for headers being passed from the platform to C to Cpp and back. Risk Level: med - changes memory management of headers. Testing: Added unit tests to make sure that headers are being freed when appropriate. Signed-off-by: Jose Nino <jnino@lyft.com> Signed-off-by: JP Simard <jp@jpsim.com>
- Loading branch information
Showing
10 changed files
with
123 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,13 @@ | ||
licenses(["notice"]) # Apache 2 | ||
|
||
load("@envoy//bazel:envoy_build_system.bzl", "envoy_cc_library", "envoy_package") | ||
|
||
envoy_package() | ||
|
||
envoy_cc_library( | ||
# This is a plain cc rule to ensure that the file is getting compiled as a plain C file, and provides plain C types. | ||
cc_library( | ||
name = "c_types_interface", | ||
srcs = [ | ||
"c_types.cc", | ||
"c_types.c", | ||
], | ||
hdrs = [ | ||
"c_types.h", | ||
], | ||
repository = "@envoy", | ||
visibility = ["//visibility:public"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include "library/common/include/c_types.h" | ||
|
||
#include <string.h> | ||
|
||
// NOLINT(namespace-envoy) | ||
// Although this file is in an envoy_cc_library rule it should be C compatible with old style casts. | ||
#pragma GCC diagnostic ignored "-Wold-style-cast" | ||
|
||
void envoy_noop_release(void* context) { (void)context; } | ||
|
||
void release_envoy_headers(envoy_headers headers) { | ||
for (envoy_header_size_t i = 0; i < headers.length; i++) { | ||
envoy_header header = headers.headers[i]; | ||
header.key.release(header.key.context); | ||
header.value.release(header.value.context); | ||
} | ||
free(headers.headers); | ||
} | ||
|
||
envoy_headers copy_envoy_headers(envoy_headers src) { | ||
envoy_header* dst_header_array = (envoy_header*)malloc(sizeof(envoy_header) * src.length); | ||
for (envoy_header_size_t i = 0; i < src.length; i++) { | ||
envoy_header new_header = { | ||
copy_envoy_data(src.headers[i].key.length, src.headers[i].key.bytes), | ||
copy_envoy_data(src.headers[i].value.length, src.headers[i].value.bytes)}; | ||
dst_header_array[i] = new_header; | ||
} | ||
envoy_headers dst = {src.length, dst_header_array}; | ||
return dst; | ||
} | ||
|
||
envoy_data copy_envoy_data(size_t length, const uint8_t* src_bytes) { | ||
uint8_t* dst_bytes = (uint8_t*)malloc(sizeof(uint8_t) * length); | ||
memcpy(dst_bytes, src_bytes, length); | ||
// Note: since this function is copying the bytes over to freshly allocated memory, free is an | ||
// appropriate release function and dst_bytes is an appropriate context. | ||
envoy_data dst = {length, dst_bytes, free, dst_bytes}; | ||
return dst; | ||
} | ||
|
||
const envoy_data envoy_nodata = {0, NULL, envoy_noop_release, NULL}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.