mailing list of musl libc
 help / color / mirror / code / Atom feed
0fab0df7634773cedeeaefbb4a01ff8d5e386dd8 blob 1868 bytes (raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
 
#define _GNU_SOURCE
#include <stdint.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <string.h>
#include "meta.h"

void *realloc(void *p, size_t n)
{
	n = ALIGN_UP(n, 16);
	if (!p) return malloc(n);
	if (size_overflows(n)) return 0;

#ifdef MEMTAG
	unsigned char *untagged = (unsigned char *)((uint64_t)p & ~MTE_TAG_MASK);
#else
	unsigned char *untagged = p;
#endif
	struct meta *g = get_meta(p);
	int idx = get_slot_index(untagged);
	size_t stride = get_stride(g);
	unsigned char *start = g->mem->storage + stride*idx;
	unsigned char *end = start + stride - IB;
	size_t old_size = get_nominal_size(untagged, end);
	size_t avail_size = end-(unsigned char *)untagged;
	void *new;

	// only resize in-place if size class matches
	if (n <= avail_size && n<MMAP_THRESHOLD
	    && size_to_class(n)+1 >= g->sizeclass) {

		uint64_t addr;

#ifdef MEMTAG
		for (size_t i = 0; i < old_size; i += 16)
			mte_store_tag((uint64_t)(untagged + i));

		uint64_t mask_mte = mte_get_exclude_mask((uint64_t)p);
		addr = mte_insert_random_tag((uint64_t)p, mask_mte);

		for (size_t i = 0; i < n; i += 16)
			mte_store_tag(addr + i);
#else
		addr = (uint64_t)p;
#endif

		set_size(untagged, end, n);

		return (void *)addr;
	}

	// use mremap if old and new size are both mmap-worthy
	if (g->sizeclass>=48 && n>=MMAP_THRESHOLD) {
		assert(g->sizeclass==63);
		size_t base = (unsigned char *)p-start;
		size_t needed = (n + base + UNIT + IB + 4095) & -4096;
		new = g->maplen*4096UL == needed ? g->mem :
			mremap(g->mem, g->maplen*4096UL, needed, MREMAP_MAYMOVE);
		if (new!=MAP_FAILED) {
			g->mem = new;
			g->maplen = needed/4096;
			p = g->mem->storage + base;
			end = g->mem->storage + (needed - UNIT) - IB;
			*end = 0;
			set_size(p, end, n);
			return p;
		}
	}

	new = malloc(n);
	if (!new) return 0;
	memcpy(new, p, n < old_size ? n : old_size);
	free(p);
	return new;
}
debug log:

solving 0fab0df7 ...
found 0fab0df7 in https://inbox.vuxu.org/musl/20240610123624.305051-2-stefanjumarea02@gmail.com/
found 18769f42 in https://git.vuxu.org/mirror/musl/
preparing index
index prepared:
100644 18769f42d83ae71dabea1e7caf117fe4115af139	src/malloc/mallocng/realloc.c

applying [1/1] https://inbox.vuxu.org/musl/20240610123624.305051-2-stefanjumarea02@gmail.com/
diff --git a/src/malloc/mallocng/realloc.c b/src/malloc/mallocng/realloc.c
index 18769f42..0fab0df7 100644

Checking patch src/malloc/mallocng/realloc.c...
Applied patch src/malloc/mallocng/realloc.c cleanly.

index at:
100644 0fab0df7634773cedeeaefbb4a01ff8d5e386dd8	src/malloc/mallocng/realloc.c

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).