mirror of
https://github.com/curl/curl.git
synced 2026-04-14 00:51:42 +08:00
Before this patch curl used the C preprocessor to override standard memory allocation symbols: malloc, calloc, strdup, realloc, free. The goal of these is to replace them with curl's debug wrappers in `CURLDEBUG` builds, another was to replace them with the wrappers calling user-defined allocators in libcurl. This solution needed a bunch of workarounds to avoid breaking external headers: it relied on include order to do the overriding last. For "unity" builds it needed to reset overrides before external includes. Also in test apps, which are always built as single source files. It also needed the `(symbol)` trick to avoid overrides in some places. This would still not fix cases where the standard symbols were macros. It was also fragile and difficult to figure out which was the actual function behind an alloc or free call in a specific piece of code. This in turn caused bugs where the wrong allocator was accidentally called. To avoid these problems, this patch replaces this solution with `curlx_`-prefixed allocator macros, and mapping them _once_ to either the libcurl wrappers, the debug wrappers or the standard ones, matching the rest of the code in libtests. This concludes the long journey to avoid redefining standard functions in the curl codebase. Note: I did not update `packages/OS400/*.c` sources. They did not `#include` `curl_setup.h`, `curl_memory.h` or `memdebug.h`, meaning the overrides were never applied to them. This may or may not have been correct. For now I suppressed the direct use of standard allocators via a local `.checksrc`. Probably they (except for `curlcl.c`) should be updated to include `curl_setup.h` and use the `curlx_` macros. This patch changes mappings in two places: - `lib/curl_threads.c` in libtests: Before this patch it mapped to libcurl allocators. After, it maps to standard allocators, like the rest of libtests code. - `units`: before this patch it mapped to standard allocators. After, it maps to libcurl allocators. Also: - drop all position-dependent `curl_memory.h` and `memdebug.h` includes, and delete the now unnecessary headers. - rename `Curl_tcsdup` macro to `curlx_tcsdup` and define like the other allocators. - map `curlx_strdup()` to `_strdup()` on Windows (was: `strdup()`). To fix warnings silenced via `_CRT_NONSTDC_NO_DEPRECATE`. - multibyte: map `curlx_convert_*()` to `_strdup()` on Windows (was: `strdup()`). - src: do not reuse the `strdup` name for the local replacement. - lib509: call `_strdup()` on Windows (was: `strdup()`). - test1132: delete test obsoleted by this patch. - CHECKSRC.md: update text for `SNPRINTF`. - checksrc: ban standard allocator symbols. Follow-up tob12da22db1#18866 Follow-up todb98daab05#18844 Follow-up to4deea9396b#18814 Follow-up to9678ff5b1b#18776 Follow-up to10bac43b87#18774 Follow-up to20142f5d06#18634 Follow-up tobf7375ecc5#18503 Follow-up to9863599d69#18502 Follow-up to3bb5e58c10#17827 Closes #19626
212 lines
5.4 KiB
C
212 lines
5.4 KiB
C
/***************************************************************************
|
|
* _ _ ____ _
|
|
* Project ___| | | | _ \| |
|
|
* / __| | | | |_) | |
|
|
* | (__| |_| | _ <| |___
|
|
* \___|\___/|_| \_\_____|
|
|
*
|
|
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
|
*
|
|
* This software is licensed as described in the file COPYING, which
|
|
* you should have received as part of this distribution. The terms
|
|
* are also available at https://curl.se/docs/copyright.html.
|
|
*
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
*
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
* KIND, either express or implied.
|
|
*
|
|
* SPDX-License-Identifier: curl
|
|
*
|
|
***************************************************************************/
|
|
|
|
#include "curl_setup.h"
|
|
#include "uint-table.h"
|
|
|
|
#ifdef DEBUGBUILD
|
|
#define CURL_UINT32_TBL_MAGIC 0x62757473
|
|
#endif
|
|
|
|
/* Clear the table, making it empty. */
|
|
UNITTEST void Curl_uint32_tbl_clear(struct uint32_tbl *tbl);
|
|
|
|
void Curl_uint32_tbl_init(struct uint32_tbl *tbl,
|
|
Curl_uint32_tbl_entry_dtor *entry_dtor)
|
|
{
|
|
memset(tbl, 0, sizeof(*tbl));
|
|
tbl->entry_dtor = entry_dtor;
|
|
tbl->last_key_added = UINT32_MAX;
|
|
#ifdef DEBUGBUILD
|
|
tbl->init = CURL_UINT32_TBL_MAGIC;
|
|
#endif
|
|
}
|
|
|
|
|
|
static void uint32_tbl_clear_rows(struct uint32_tbl *tbl,
|
|
uint32_t from,
|
|
uint32_t upto_excluding)
|
|
{
|
|
uint32_t i, end;
|
|
|
|
end = CURLMIN(upto_excluding, tbl->nrows);
|
|
for(i = from; i < end; ++i) {
|
|
if(tbl->rows[i]) {
|
|
if(tbl->entry_dtor)
|
|
tbl->entry_dtor(i, tbl->rows[i]);
|
|
tbl->rows[i] = NULL;
|
|
tbl->nentries--;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
CURLcode Curl_uint32_tbl_resize(struct uint32_tbl *tbl, uint32_t nrows)
|
|
{
|
|
/* we use `tbl->nrows + 1` during iteration, want that to work */
|
|
DEBUGASSERT(tbl->init == CURL_UINT32_TBL_MAGIC);
|
|
if(!nrows)
|
|
return CURLE_BAD_FUNCTION_ARGUMENT;
|
|
if(nrows != tbl->nrows) {
|
|
void **rows = curlx_calloc(nrows, sizeof(void *));
|
|
if(!rows)
|
|
return CURLE_OUT_OF_MEMORY;
|
|
if(tbl->rows) {
|
|
memcpy(rows, tbl->rows, (CURLMIN(nrows, tbl->nrows) * sizeof(void *)));
|
|
if(nrows < tbl->nrows)
|
|
uint32_tbl_clear_rows(tbl, nrows, tbl->nrows);
|
|
curlx_free(tbl->rows);
|
|
}
|
|
tbl->rows = rows;
|
|
tbl->nrows = nrows;
|
|
}
|
|
return CURLE_OK;
|
|
}
|
|
|
|
|
|
void Curl_uint32_tbl_destroy(struct uint32_tbl *tbl)
|
|
{
|
|
DEBUGASSERT(tbl->init == CURL_UINT32_TBL_MAGIC);
|
|
Curl_uint32_tbl_clear(tbl);
|
|
curlx_free(tbl->rows);
|
|
memset(tbl, 0, sizeof(*tbl));
|
|
}
|
|
|
|
UNITTEST void Curl_uint32_tbl_clear(struct uint32_tbl *tbl)
|
|
{
|
|
DEBUGASSERT(tbl->init == CURL_UINT32_TBL_MAGIC);
|
|
uint32_tbl_clear_rows(tbl, 0, tbl->nrows);
|
|
DEBUGASSERT(!tbl->nentries);
|
|
tbl->last_key_added = UINT32_MAX;
|
|
}
|
|
|
|
|
|
uint32_t Curl_uint32_tbl_capacity(struct uint32_tbl *tbl)
|
|
{
|
|
return tbl->nrows;
|
|
}
|
|
|
|
|
|
uint32_t Curl_uint32_tbl_count(struct uint32_tbl *tbl)
|
|
{
|
|
return tbl->nentries;
|
|
}
|
|
|
|
|
|
void *Curl_uint32_tbl_get(struct uint32_tbl *tbl, uint32_t key)
|
|
{
|
|
return (key < tbl->nrows) ? tbl->rows[key] : NULL;
|
|
}
|
|
|
|
|
|
bool Curl_uint32_tbl_add(struct uint32_tbl *tbl, void *entry, uint32_t *pkey)
|
|
{
|
|
uint32_t key, start_pos;
|
|
|
|
DEBUGASSERT(tbl->init == CURL_UINT32_TBL_MAGIC);
|
|
if(!entry || !pkey)
|
|
return FALSE;
|
|
*pkey = UINT32_MAX;
|
|
if(tbl->nentries == tbl->nrows) /* full */
|
|
return FALSE;
|
|
|
|
start_pos = CURLMIN(tbl->last_key_added, tbl->nrows) + 1;
|
|
for(key = start_pos; key < tbl->nrows; ++key) {
|
|
if(!tbl->rows[key]) {
|
|
tbl->rows[key] = entry;
|
|
tbl->nentries++;
|
|
tbl->last_key_added = key;
|
|
*pkey = key;
|
|
return TRUE;
|
|
}
|
|
}
|
|
/* no free entry at or above tbl->maybe_next_key, wrap around */
|
|
for(key = 0; key < start_pos; ++key) {
|
|
if(!tbl->rows[key]) {
|
|
tbl->rows[key] = entry;
|
|
tbl->nentries++;
|
|
tbl->last_key_added = key;
|
|
*pkey = key;
|
|
return TRUE;
|
|
}
|
|
}
|
|
/* Did not find any free row? Should not happen */
|
|
DEBUGASSERT(0);
|
|
return FALSE;
|
|
}
|
|
|
|
|
|
void Curl_uint32_tbl_remove(struct uint32_tbl *tbl, uint32_t key)
|
|
{
|
|
uint32_tbl_clear_rows(tbl, key, key + 1);
|
|
}
|
|
|
|
|
|
bool Curl_uint32_tbl_contains(struct uint32_tbl *tbl, uint32_t key)
|
|
{
|
|
return (key < tbl->nrows) ? !!tbl->rows[key] : FALSE;
|
|
}
|
|
|
|
|
|
static bool uint32_tbl_next_at(struct uint32_tbl *tbl, uint32_t key,
|
|
uint32_t *pkey, void **pentry)
|
|
{
|
|
for(; key < tbl->nrows; ++key) {
|
|
if(tbl->rows[key]) {
|
|
*pkey = key;
|
|
*pentry = tbl->rows[key];
|
|
return TRUE;
|
|
}
|
|
}
|
|
*pkey = UINT32_MAX; /* always invalid */
|
|
*pentry = NULL;
|
|
return FALSE;
|
|
}
|
|
|
|
bool Curl_uint32_tbl_first(struct uint32_tbl *tbl,
|
|
uint32_t *pkey, void **pentry)
|
|
{
|
|
if(!pkey || !pentry)
|
|
return FALSE;
|
|
if(tbl->nentries && uint32_tbl_next_at(tbl, 0, pkey, pentry))
|
|
return TRUE;
|
|
DEBUGASSERT(!tbl->nentries);
|
|
*pkey = UINT32_MAX; /* always invalid */
|
|
*pentry = NULL;
|
|
return FALSE;
|
|
}
|
|
|
|
|
|
bool Curl_uint32_tbl_next(struct uint32_tbl *tbl, uint32_t last_key,
|
|
uint32_t *pkey, void **pentry)
|
|
{
|
|
if(!pkey || !pentry)
|
|
return FALSE;
|
|
if(uint32_tbl_next_at(tbl, last_key + 1, pkey, pentry))
|
|
return TRUE;
|
|
*pkey = UINT32_MAX; /* always invalid */
|
|
*pentry = NULL;
|
|
return FALSE;
|
|
}
|