MCPcopy Create free account
hub / github.com/CppCXY/EmmyLuaCodeStyle / mi_heap_strndup

Function mi_heap_strndup

3rd/mimalloc-2.0.9/src/alloc.c:781–791  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

779
780// `strndup` using mi_malloc
781mi_decl_nodiscard mi_decl_restrict char* mi_heap_strndup(mi_heap_t* heap, const char* s, size_t n) mi_attr_noexcept {
782 if (s == NULL) return NULL;
783 const char* end = (const char*)memchr(s, 0, n); // find end of string in the first `n` characters (returns NULL if not found)
784 const size_t m = (end != NULL ? (size_t)(end - s) : n); // `m` is the minimum of `n` or the end-of-string
785 mi_assert_internal(m <= n);
786 char* t = (char*)mi_heap_malloc(heap, m+1);
787 if (t == NULL) return NULL;
788 _mi_memcpy(t, s, m);
789 t[m] = 0;
790 return t;
791}
792
793mi_decl_nodiscard mi_decl_restrict char* mi_strndup(const char* s, size_t n) mi_attr_noexcept {
794 return mi_heap_strndup(mi_get_default_heap(),s,n);

Callers 2

mi_strndupFunction · 0.85
mi_heap_realpathFunction · 0.85

Calls 2

mi_heap_mallocFunction · 0.85
_mi_memcpyFunction · 0.85

Tested by

no test coverage detected