Skip to content

[CF] Avoid using malloc introspection. #2626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 27, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[CF] Do not call malloc_size unless debugging.
CFBasicHashGetSize has an argument, total, to enable whether to
presumably calculate the size its dependent data structures.
CFBasicHashGetSize does not appear outside CFBasicHash.c, and this total
argument is true only when ENABLE_MEMORY_COUNTERS is true or
transitively when ENABLE_DTRACE_PROBES is true. These preprocessor
symbols appear to be to control debugging and other informational
purposes.

malloc introspection, as afforded by malloc_size, malloc_usable_size, or
msize, is nonportable (and thus not guaranteed to be present on all
platforms), not guaranteed to be accurate (though certainly an upper
bound), and is generally recommended for debugging.

For those reasons, only expose the call to malloc_size when either of
ENABLE_MEMORY_COUNTERS or ENABLE_DTRACE_PROBES is true. This avoids an
undefined symbol issue when malloc introspection is unavailable, but
does mean these counters and other information is not available on
platforms when malloc_size is not present.
  • Loading branch information
3405691582 committed Jan 22, 2020
commit 1af7c8dfaf559ff6f47b7ffd60126e834c92ab4a
4 changes: 4 additions & 0 deletions CoreFoundation/Collections.subproj/CFBasicHash.c
Original file line number Diff line number Diff line change
Expand Up @@ -1442,6 +1442,7 @@ CF_PRIVATE size_t CFBasicHashGetSize(CFConstBasicHashRef ht, Boolean total) {
if (ht->bits.keys_offset) size += sizeof(CFBasicHashValue *);
if (ht->bits.counts_offset) size += sizeof(void *);
if (__CFBasicHashHasHashCache(ht)) size += sizeof(uintptr_t *);
#if ENABLE_MEMORY_COUNTERS || ENABLE_DTRACE_PROBES
if (total) {
CFIndex num_buckets = __CFBasicHashTableSizes[ht->bits.num_buckets_idx];
if (0 < num_buckets) {
Expand All @@ -1451,6 +1452,9 @@ CF_PRIVATE size_t CFBasicHashGetSize(CFConstBasicHashRef ht, Boolean total) {
if (__CFBasicHashHasHashCache(ht)) size += malloc_size(__CFBasicHashGetHashes(ht));
}
}
#else
(void)total;
#endif
return size;
}

Expand Down