This is an automated email from the ASF dual-hosted git repository.
wkaras pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git
The following commit(s) were added to refs/heads/master by this push:
new e449e85701 Change malloc template to TSRalloc (fixes Ubuntu build).
(#11069)
e449e85701 is described below
commit e449e857019ee9aff7d25489560c7cb78bed1578
Author: Walt Karas <[email protected]>
AuthorDate: Tue Feb 13 12:55:40 2024 -0500
Change malloc template to TSRalloc (fixes Ubuntu build). (#11069)
* Change malloc template to TSRalloc (fixes Ubuntu build).
* Changes to docs to remove tsapi (and thus tsapi::c) namespaces.
* Fix spelling error in ts.h comment.
---
doc/developer-guide/api/functions/TSDebug.en.rst | 3 ---
doc/developer-guide/api/functions/TSmalloc.en.rst | 10 +++++-----
.../plugins/getting-started/naming-conventions.en.rst | 11 ++---------
example/plugins/c-api/denylist_0/denylist_0.cc | 2 +-
example/plugins/c-api/redirect_1/redirect_1.cc | 2 +-
example/plugins/c-api/thread_pool/psi.cc | 2 +-
example/plugins/c-api/thread_pool/thread.cc | 4 ++--
include/ts/ts.h | 8 +++++++-
plugins/experimental/fq_pacing/fq_pacing.cc | 4 ++--
plugins/experimental/url_sig/url_sig.cc | 2 +-
plugins/healthchecks/healthchecks.cc | 12 ++++++------
plugins/libloader/libloader.cc | 2 +-
plugins/lua/ts_lua.cc | 8 ++++----
plugins/lua/ts_lua_client_response.cc | 4 ++--
plugins/lua/ts_lua_crypto.cc | 6 +++---
plugins/lua/ts_lua_util.cc | 12 ++++++------
plugins/remap_purge/remap_purge.cc | 2 +-
17 files changed, 45 insertions(+), 49 deletions(-)
diff --git a/doc/developer-guide/api/functions/TSDebug.en.rst
b/doc/developer-guide/api/functions/TSDebug.en.rst
index 460cc928b0..c98255df66 100644
--- a/doc/developer-guide/api/functions/TSDebug.en.rst
+++ b/doc/developer-guide/api/functions/TSDebug.en.rst
@@ -107,9 +107,6 @@ is turned off.
string representation. This can be useful in debugging (cpp:func:`Dbg`),
logging and other types notifications.
-(Note that cpp:type:`DbgCtl`, cpp:func:`Dbg`, cpp:func:`SpecificDbg` and
cpp:func:`DbgPrint`
-are not in the ``tsapi::c`` namespace.)
-
(For an example of how to write a plugin with debug tracing, that can be
compiled with both |TS| Version 10 and older versions of ATS, see
``redirect_1``.)
diff --git a/doc/developer-guide/api/functions/TSmalloc.en.rst
b/doc/developer-guide/api/functions/TSmalloc.en.rst
index b031874fdf..022fe37c95 100644
--- a/doc/developer-guide/api/functions/TSmalloc.en.rst
+++ b/doc/developer-guide/api/functions/TSmalloc.en.rst
@@ -32,7 +32,7 @@ Synopsis
#include <ts/ts.h>
.. function:: void * TSmalloc(size_t size)
-.. cpp:function:: template <typename T> T * malloc(size_t count = 1)
+.. cpp:function:: template <typename T> T * TSRalloc(size_t count = 1)
.. function:: void * TSrealloc(void * ptr , size_t size)
.. function:: char * TSstrdup(const char * str)
.. function:: char * TSstrndup(const char * str, size_t size)
@@ -59,11 +59,11 @@ heap. Traffic Server uses :func:`TSmalloc` internally for
memory allocations.
Always use :func:`TSfree` to release memory allocated by :func:`TSmalloc`; do
not use
:code:`free`.
-**malloc()**, which is in the :code:`tsapi` namespace, returns a pointer, of
type :code:`T *`,
+**TSRalloc()** returns a pointer, of type :code:`T *`,
to allocated memory with enough bytes to hold an array of :code:`count`
(default value
-of :code;`1`) instances of :code:`T`. No constructor of :code:`T` is called
for the
-array elements. This function in turn calls :func:`TSmalloc`, so the memory
it allocates
-should be released by calling :func:`TSfree`.
+of :code;`1`) instances of :code:`T`. The memory is "raw", no constructor of
:code:`T`
+is called for the array elements. This function in turn calls
:func:`TSmalloc`, so the
+memory it allocates should be released by calling :func:`TSfree`.
:func:`TSstrdup` returns a pointer to a new string that is a duplicate of the
string pointed to by str. The memory for the new string is allocated using
diff --git
a/doc/developer-guide/plugins/getting-started/naming-conventions.en.rst
b/doc/developer-guide/plugins/getting-started/naming-conventions.en.rst
index ba2aeddfc3..8824977418 100644
--- a/doc/developer-guide/plugins/getting-started/naming-conventions.en.rst
+++ b/doc/developer-guide/plugins/getting-started/naming-conventions.en.rst
@@ -19,18 +19,11 @@
.. _developer-plugins-getting-started-naming:
-Naming/namespace Conventions
-****************************
+Naming Conventions
+******************
The Traffic Server API adheres to the following naming conventions:
-- Unless otherwise noted, all C++ declarations provided by the API are in
- the ``tsapi::c`` namspace. However, there are ``using namespace tsapi::c``
- directives in the appropriate API header files, so explicit use of this
- namespace is generally unnecessary. (The ``c`` sub-namespace is used because
- the design of the API reflects the fact that it was originally a straight
- C API. As new declarations, designed to use C++ features, are added to the
- API, they will be placed directly in the ``tsapi`` namespace.)
- The ``TS`` prefix is used for all function and variable names defined
in the Traffic Server API. **Examples**:
``TS_EVENT_NONE``,\ ``TSMutex``, and ``TSContCreate``
diff --git a/example/plugins/c-api/denylist_0/denylist_0.cc
b/example/plugins/c-api/denylist_0/denylist_0.cc
index 5c2fcac0d0..364a5d31cb 100644
--- a/example/plugins/c-api/denylist_0/denylist_0.cc
+++ b/example/plugins/c-api/denylist_0/denylist_0.cc
@@ -115,7 +115,7 @@ handle_response(TSHttpTxn txnp)
goto done;
}
- buf = malloc<char>(4096);
+ buf = TSRalloc<char>(4096);
url_str = TSUrlStringGet(bufp, url_loc, &url_length);
snprintf(buf, 4096, "You are forbidden from accessing \"%s\"\n", url_str);
diff --git a/example/plugins/c-api/redirect_1/redirect_1.cc
b/example/plugins/c-api/redirect_1/redirect_1.cc
index ce3bfa0917..10c1d9c025 100644
--- a/example/plugins/c-api/redirect_1/redirect_1.cc
+++ b/example/plugins/c-api/redirect_1/redirect_1.cc
@@ -333,7 +333,7 @@ TSPluginInit(int argc, const char *argv[])
url_redirect = TSstrdup(argv[2]);
int uri_len = strlen(prefix) + strlen(url_redirect) + 1;
- uri_redirect = malloc<char>(uri_len);
+ uri_redirect = TSRalloc<char>(uri_len);
TSstrlcpy(uri_redirect, prefix, uri_len);
TSstrlcat(uri_redirect, url_redirect, uri_len);
diff --git a/example/plugins/c-api/thread_pool/psi.cc
b/example/plugins/c-api/thread_pool/psi.cc
index 382f7c79ef..89e96fefdc 100644
--- a/example/plugins/c-api/thread_pool/psi.cc
+++ b/example/plugins/c-api/thread_pool/psi.cc
@@ -746,7 +746,7 @@ transform_handler(TSCont contp, TSEvent event, void *edata
ATS_UNUSED)
/* Handle TryLock result */
if (TSMutexLockTry(TSContMutexGet(contp)) != TS_SUCCESS) {
TSCont c = TSContCreate(trylock_handler, nullptr);
- auto d = malloc<TryLockData>();
+ auto d = TSRalloc<TryLockData>();
d->contp = contp;
d->event = event;
diff --git a/example/plugins/c-api/thread_pool/thread.cc
b/example/plugins/c-api/thread_pool/thread.cc
index 9fd9b733a3..1df48c5507 100644
--- a/example/plugins/c-api/thread_pool/thread.cc
+++ b/example/plugins/c-api/thread_pool/thread.cc
@@ -51,7 +51,7 @@ add_to_queue(Queue *q, void *data)
if (data != nullptr) {
TSMutexLock(q->mutex);
/* Init the new cell */
- auto new_cell = malloc<Cell>();
+ auto new_cell = TSRalloc<Cell>();
new_cell->magic = MAGIC_ALIVE;
new_cell->ptr_data = data;
new_cell->ptr_next = q->tail;
@@ -120,7 +120,7 @@ get_nbelem_queue(Queue *q)
Job *
job_create(TSCont contp, ExecFunc func, void *data)
{
- auto new_job = malloc<Job>();
+ auto new_job = TSRalloc<Job>();
new_job->magic = MAGIC_ALIVE;
new_job->cont = contp;
new_job->func = func;
diff --git a/include/ts/ts.h b/include/ts/ts.h
index 02139024cd..b244610a8b 100644
--- a/include/ts/ts.h
+++ b/include/ts/ts.h
@@ -3083,9 +3083,15 @@ TSAction TSNetConnectTransparent(
struct sockaddr const *to /**< Address to which to connect. */
);
+/**
+ Allocates contiguous, aligned, raw (no construction) memory for a given
number number of instances of type T.
+
+ @return Pointer to raw (in spite of pointer type) memory for first instance.
+*/
template <typename T>
T *
-malloc(size_t count = 1)
+TSRalloc(size_t count = 1 /**< Number of instances of T to allocate storage
for. */
+)
{
return static_cast<std::remove_cv_t<T> *>(TSmalloc(count * sizeof(T)));
}
diff --git a/plugins/experimental/fq_pacing/fq_pacing.cc
b/plugins/experimental/fq_pacing/fq_pacing.cc
index 00480093d0..f1e311b9e6 100644
--- a/plugins/experimental/fq_pacing/fq_pacing.cc
+++ b/plugins/experimental/fq_pacing/fq_pacing.cc
@@ -149,7 +149,7 @@ TSRemapNewInstance(int argc, char *argv[], void **ih, char
*errbuf, int errbuf_s
return TS_ERROR;
}
- cfg = malloc<fq_pacing_cfg_t>();
+ cfg = TSRalloc<fq_pacing_cfg_t>();
memset(cfg, 0, sizeof(*cfg));
cfg->pacing_rate = pacing_rate;
*ih = cfg;
@@ -223,7 +223,7 @@ TSRemapDoRemap(void *instance, TSHttpTxn txnp,
TSRemapRequestInfo *rri)
// reused for another delivery service w/o pacing
TSCont cont = TSContCreate(reset_pacing_cont, nullptr);
- auto txn_data = malloc<fq_pacing_cont_t>();
+ auto txn_data = TSRalloc<fq_pacing_cont_t>();
txn_data->client_fd = client_fd;
TSContDataSet(cont, txn_data);
diff --git a/plugins/experimental/url_sig/url_sig.cc
b/plugins/experimental/url_sig/url_sig.cc
index 33644fd80e..1a781403c8 100644
--- a/plugins/experimental/url_sig/url_sig.cc
+++ b/plugins/experimental/url_sig/url_sig.cc
@@ -122,7 +122,7 @@ TSRemapNewInstance(int argc, char *argv[], void **ih, char
*errbuf, int errbuf_s
int keynum;
bool eat_comment = false;
- cfg = malloc<config>();
+ cfg = TSRalloc<config>();
memset(cfg, 0, sizeof(struct config));
while (fgets(line, sizeof(line), file) != nullptr) {
diff --git a/plugins/healthchecks/healthchecks.cc
b/plugins/healthchecks/healthchecks.cc
index bc5dd47f86..5644d8b182 100644
--- a/plugins/healthchecks/healthchecks.cc
+++ b/plugins/healthchecks/healthchecks.cc
@@ -145,7 +145,7 @@ setup_watchers(int fd)
/* Make sure to only watch each directory once */
if (!(dir = find_direntry(dname, head_dir))) {
Dbg(dbg_ctl, "Setting up a watcher for directory %s", dname);
- dir = malloc<HCDirEntry>();
+ dir = TSRalloc<HCDirEntry>();
memset(dir, 0, sizeof(HCDirEntry));
strncpy(dir->dname, dname, MAX_PATH_LEN - 1);
dir->wd = inotify_add_watch(fd, dname, IN_CREATE | IN_MOVED_FROM |
IN_MOVED_TO | IN_ATTRIB);
@@ -225,7 +225,7 @@ hc_thread(void *data ATS_UNUSED)
finfo = finfo->_next;
}
if (finfo) {
- auto *new_data = malloc<HCFileData>();
+ auto *new_data = TSRalloc<HCFileData>();
HCFileData *old_data;
if (event->mask & (IN_CLOSE_WRITE | IN_ATTRIB)) {
@@ -274,7 +274,7 @@ gen_header(char *status_str, char *mime, int *header_len)
status_reason = TSHttpHdrReasonLookup(status);
len += strlen(status_reason);
len += strlen(mime);
- buf = malloc<char>(len);
+ buf = TSRalloc<char>(len);
*header_len = snprintf(buf, len, HEADER_TEMPLATE, status,
status_reason, mime);
} else {
*header_len = 0;
@@ -312,7 +312,7 @@ parse_configs(const char *fname)
char *str, *save;
char *ok = nullptr, *miss = nullptr, *mime = nullptr;
- finfo = malloc<HCFileInfo>();
+ finfo = TSRalloc<HCFileInfo>();
memset(static_cast<void *>(finfo), 0, sizeof(HCFileInfo));
if (fgets(buf, sizeof(buf) - 1, fd)) {
@@ -358,7 +358,7 @@ parse_configs(const char *fname)
Dbg(dbg_ctl, "Parsed: %s %s %s %s %s", finfo->path, finfo->fname,
mime, ok, miss);
finfo->ok = gen_header(ok, mime, &finfo->o_len);
finfo->miss = gen_header(miss, mime, &finfo->m_len);
- finfo->data = malloc<HCFileData>();
+ finfo->data = TSRalloc<HCFileData>();
memset(finfo->data, 0, sizeof(HCFileData));
reload_status_file(finfo, finfo->data);
@@ -525,7 +525,7 @@ health_check_origin(TSCont contp ATS_UNUSED, TSEvent event
ATS_UNUSED, void *eda
/* This is us -- register our intercept */
icontp = TSContCreate(hc_intercept, TSMutexCreate());
- my_state = malloc<HCState>();
+ my_state = TSRalloc<HCState>();
memset(my_state, 0, sizeof(*my_state));
my_state->info = info;
my_state->data = info->data;
diff --git a/plugins/libloader/libloader.cc b/plugins/libloader/libloader.cc
index 316c8478ea..32fa9218e7 100644
--- a/plugins/libloader/libloader.cc
+++ b/plugins/libloader/libloader.cc
@@ -75,7 +75,7 @@ TSPluginInit(int argc, const char *argv[])
const char *lib = argv[i];
void *handle = dlopen(lib, RTLD_GLOBAL | RTLD_NOW);
if (handle) {
- auto l = malloc<link_handle>();
+ auto l = TSRalloc<link_handle>();
l->handle = handle;
l->next = libs;
libs = l;
diff --git a/plugins/lua/ts_lua.cc b/plugins/lua/ts_lua.cc
index fa0e980b19..339adf72c8 100644
--- a/plugins/lua/ts_lua.cc
+++ b/plugins/lua/ts_lua.cc
@@ -86,7 +86,7 @@ typedef struct {
ts_lua_plugin_stats *
create_plugin_stats(ts_lua_main_ctx *const main_ctx_array, char const *const
*stat_strs)
{
- auto *const stats = malloc<ts_lua_plugin_stats>();
+ auto *const stats = TSRalloc<ts_lua_plugin_stats>();
memset(stats, 0, sizeof(ts_lua_plugin_stats));
stats->main_ctx_array = main_ctx_array;
@@ -147,7 +147,7 @@ create_lua_vms()
}
}
- ctx_array = malloc<ts_lua_main_ctx>(ts_lua_max_state_count);
+ ctx_array = TSRalloc<ts_lua_main_ctx>(ts_lua_max_state_count);
memset(ctx_array, 0, sizeof(ts_lua_main_ctx) * ts_lua_max_state_count);
int const ret = ts_lua_create_vm(ctx_array, ts_lua_max_state_count);
@@ -437,7 +437,7 @@ TSRemapNewInstance(int argc, char *argv[], void **ih, char
*errbuf, int errbuf_s
if (!conf) {
Dbg(dbg_ctl, "[%s] creating new conf instance", __FUNCTION__);
- conf = malloc<ts_lua_instance_conf>();
+ conf = TSRalloc<ts_lua_instance_conf>();
if (!conf) {
strncpy(errbuf, "[TSRemapNewInstance] malloc failed!!", errbuf_size - 1);
errbuf[errbuf_size - 1] = '\0';
@@ -920,7 +920,7 @@ TSPluginInit(int argc, const char *argv[])
return;
}
- auto *conf = malloc<ts_lua_instance_conf>();
+ auto *conf = TSRalloc<ts_lua_instance_conf>();
if (!conf) {
TSError("[ts_lua][%s] malloc failed !!", __FUNCTION__);
return;
diff --git a/plugins/lua/ts_lua_client_response.cc
b/plugins/lua/ts_lua_client_response.cc
index 20a981afd3..8b6bfdc160 100644
--- a/plugins/lua/ts_lua_client_response.cc
+++ b/plugins/lua/ts_lua_client_response.cc
@@ -493,12 +493,12 @@ ts_lua_client_response_set_error_resp(lua_State *L)
}
if (body_len && body) {
- resp_buf = malloc<char>(body_len);
+ resp_buf = TSRalloc<char>(body_len);
memcpy(resp_buf, body, body_len);
resp_len = body_len;
} else {
- resp_buf = malloc<char>(reason_len);
+ resp_buf = TSRalloc<char>(reason_len);
memcpy(resp_buf, reason, reason_len);
resp_len = reason_len;
}
diff --git a/plugins/lua/ts_lua_crypto.cc b/plugins/lua/ts_lua_crypto.cc
index 1efb6987c6..011e94c8af 100644
--- a/plugins/lua/ts_lua_crypto.cc
+++ b/plugins/lua/ts_lua_crypto.cc
@@ -323,7 +323,7 @@ ts_lua_hmac_md5(lua_State *L)
src = (u_char *)luaL_checklstring(L, 2, &slen);
}
- key_bin = malloc<unsigned char>((klen / 2) + 1);
+ key_bin = TSRalloc<unsigned char>((klen / 2) + 1);
if (key_bin == nullptr) {
Dbg(dbg_ctl, "unable to allocate buffer for hex to binary conversion");
return luaL_error(L, "unable to allocate buffer for hex to binary
conversion");
@@ -378,7 +378,7 @@ ts_lua_hmac_sha1(lua_State *L)
src = (u_char *)luaL_checklstring(L, 2, &slen);
}
- key_bin = malloc<unsigned char>((klen / 2) + 1);
+ key_bin = TSRalloc<unsigned char>((klen / 2) + 1);
if (key_bin == nullptr) {
Dbg(dbg_ctl, "unable to allocate buffer for hex to binary conversion");
return luaL_error(L, "unable to allocate buffer for hex to binary
conversion");
@@ -433,7 +433,7 @@ ts_lua_hmac_sha256(lua_State *L)
src = (u_char *)luaL_checklstring(L, 2, &slen);
}
- key_bin = malloc<unsigned char>((klen / 2) + 1);
+ key_bin = TSRalloc<unsigned char>((klen / 2) + 1);
if (key_bin == nullptr) {
Dbg(dbg_ctl, "unable to allocate buffer for hex to binary conversion");
return luaL_error(L, "unable to allocate buffer for hex to binary
conversion");
diff --git a/plugins/lua/ts_lua_util.cc b/plugins/lua/ts_lua_util.cc
index 05e82bb29f..f26bc883ac 100644
--- a/plugins/lua/ts_lua_util.cc
+++ b/plugins/lua/ts_lua_util.cc
@@ -185,7 +185,7 @@ ts_lua_new_state()
ts_lua_ctx_stats *
ts_lua_create_ctx_stats()
{
- auto stats = malloc<ts_lua_ctx_stats>();
+ auto stats = TSRalloc<ts_lua_ctx_stats>();
memset(stats, 0, sizeof(*stats));
stats->mutexp = TSMutexCreate();
@@ -619,7 +619,7 @@ ts_lua_create_async_ctx(lua_State *L, ts_lua_cont_info
*hci, int n)
ts_lua_coroutine *crt;
ts_lua_http_ctx *actx;
- actx = malloc<ts_lua_http_ctx>();
+ actx = TSRalloc<ts_lua_http_ctx>();
memset(actx, 0, sizeof(*actx));
// create lua_thread
@@ -698,7 +698,7 @@ ts_lua_create_vconn_ctx(ts_lua_main_ctx *main_ctx,
ts_lua_instance_conf *conf)
{
ts_lua_vconn_ctx *vconn_ctx;
- vconn_ctx = malloc<ts_lua_vconn_ctx>();
+ vconn_ctx = TSRalloc<ts_lua_vconn_ctx>();
memset(vconn_ctx, 0, sizeof(*vconn_ctx));
lua_State *L = main_ctx->lua;
@@ -792,7 +792,7 @@ ts_lua_create_http_ctx(ts_lua_main_ctx *main_ctx,
ts_lua_instance_conf *conf)
L = main_ctx->lua;
- http_ctx = malloc<ts_lua_http_ctx>();
+ http_ctx = TSRalloc<ts_lua_http_ctx>();
memset(http_ctx, 0, sizeof(*http_ctx));
// create coroutine for http_ctx
@@ -920,7 +920,7 @@ ts_lua_create_http_intercept_ctx(lua_State *L,
ts_lua_http_ctx *http_ctx, int n)
hci = &http_ctx->cinfo;
- ictx = malloc<ts_lua_http_intercept_ctx>();
+ ictx = TSRalloc<ts_lua_http_intercept_ctx>();
memset(ictx, 0, sizeof(*ictx));
ictx->hctx = http_ctx;
@@ -1000,7 +1000,7 @@ ts_lua_create_http_transform_ctx(ts_lua_http_ctx
*http_ctx, TSVConn connp)
hci = &http_ctx->cinfo;
L = hci->routine.lua;
- transform_ctx = malloc<ts_lua_http_transform_ctx>();
+ transform_ctx = TSRalloc<ts_lua_http_transform_ctx>();
memset(transform_ctx, 0, sizeof(*transform_ctx));
transform_ctx->hctx = http_ctx;
diff --git a/plugins/remap_purge/remap_purge.cc
b/plugins/remap_purge/remap_purge.cc
index 927ec7ced2..7b8ad478b8 100644
--- a/plugins/remap_purge/remap_purge.cc
+++ b/plugins/remap_purge/remap_purge.cc
@@ -257,7 +257,7 @@ TSReturnCode
TSRemapNewInstance(int argc, char *argv[], void **ih, char *errbuf, int
errbuf_size)
{
char *id = argv[0]; /* The ID is default to the
"from" URL, so save it */
- auto purge = malloc<PurgeInstance>();
+ auto purge = TSRalloc<PurgeInstance>();
static const struct option longopt[] = {
{(char *)"id", required_argument, nullptr, 'i' },
{(char *)"secret", required_argument, nullptr, 's' },