Skip to content

Commit 19517e4

Browse files
committed
replace obmalloc's homegrown uptr and uchar types with standard ones
1 parent 3924f93 commit 19517e4

1 file changed

Lines changed: 31 additions & 42 deletions

File tree

‎Objects/obmalloc.c‎

Lines changed: 31 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,9 @@ extern void _PyMem_DumpTraceback(int fd, const void *ptr);
99

1010
/* Python's malloc wrappers (see pymem.h) */
1111

12-
/*
13-
* Basic types
14-
* I don't care if these are defined in <sys/types.h> or elsewhere. Axiom.
15-
*/
16-
#undef uchar
17-
#define uchar unsigned char /* assuming == 8 bits */
18-
1912
#undef uint
2013
#define uint unsigned int /* assuming >= 16 bits */
2114

22-
#undef uptr
23-
#define uptr uintptr_t
24-
2515
/* Forward declaration */
2616
static void* _PyMem_DebugRawMalloc(void *ctx, size_t size);
2717
static void* _PyMem_DebugRawCalloc(void *ctx, size_t nelem, size_t elsize);
@@ -746,7 +736,7 @@ static int running_on_valgrind = -1;
746736
#define SIMPLELOCK_UNLOCK(lock) /* release acquired lock */
747737

748738
/* When you say memory, my mind reasons in terms of (pointers to) blocks */
749-
typedef uchar block;
739+
typedef uint8_t block;
750740

751741
/* Pool for small blocks. */
752742
struct pool_header {
@@ -770,7 +760,7 @@ struct arena_object {
770760
* here to mark an arena_object that doesn't correspond to an
771761
* allocated arena.
772762
*/
773-
uptr address;
763+
uinptr_t address;
774764

775765
/* Pool-aligned pointer to the next pool to be carved off. */
776766
block* pool_address;
@@ -921,7 +911,7 @@ on that C doesn't insert any padding anywhere in a pool_header at or before
921911
the prevpool member.
922912
**************************************************************************** */
923913

924-
#define PTA(x) ((poolp )((uchar *)&(usedpools[2*(x)]) - 2*sizeof(block *)))
914+
#define PTA(x) ((poolp )((uint8_t *)&(usedpools[2*(x)]) - 2*sizeof(block *)))
925915
#define PT(x) PTA(x), PTA(x)
926916

927917
static poolp usedpools[2 * ((NB_SMALL_SIZE_CLASSES + 7) / 8) * 8] = {
@@ -1101,7 +1091,7 @@ new_arena(void)
11011091
unused_arena_objects = arenaobj;
11021092
return NULL;
11031093
}
1104-
arenaobj->address = (uptr)address;
1094+
arenaobj->address = (uinptr_t)address;
11051095

11061096
++narenas_currently_allocated;
11071097
++ntimes_arena_allocated;
@@ -1208,7 +1198,7 @@ address_in_range(void *p, poolp pool)
12081198
// only once.
12091199
uint arenaindex = *((volatile uint *)&pool->arenaindex);
12101200
return arenaindex < maxarenas &&
1211-
(uptr)p - arenas[arenaindex].address < ARENA_SIZE &&
1201+
(uinptr_t)p - arenas[arenaindex].address < ARENA_SIZE &&
12121202
arenas[arenaindex].address != 0;
12131203
}
12141204

@@ -1796,7 +1786,7 @@ bumpserialno(void)
17961786
static size_t
17971787
read_size_t(const void *p)
17981788
{
1799-
const uchar *q = (const uchar *)p;
1789+
const uint8_t *q = (const uint8_t *)p;
18001790
size_t result = *q++;
18011791
int i;
18021792

@@ -1811,11 +1801,11 @@ read_size_t(const void *p)
18111801
static void
18121802
write_size_t(void *p, size_t n)
18131803
{
1814-
uchar *q = (uchar *)p + SST - 1;
1804+
uint8_t *q = (uint8_t *)p + SST - 1;
18151805
int i;
18161806

18171807
for (i = SST; --i >= 0; --q) {
1818-
*q = (uchar)(n & 0xff);
1808+
*q = (uint8_t)(n & 0xff);
18191809
n >>= 8;
18201810
}
18211811
}
@@ -1850,8 +1840,8 @@ static void *
18501840
_PyMem_DebugRawAlloc(int use_calloc, void *ctx, size_t nbytes)
18511841
{
18521842
debug_alloc_api_t *api = (debug_alloc_api_t *)ctx;
1853-
uchar *p; /* base address of malloc'ed block */
1854-
uchar *tail; /* p + 2*SST + nbytes == pointer to tail pad bytes */
1843+
uint8_t *p; /* base address of malloc'ed block */
1844+
uint8_t *tail; /* p + 2*SST + nbytes == pointer to tail pad bytes */
18551845
size_t total; /* nbytes + 4*SST */
18561846

18571847
bumpserialno();
@@ -1861,15 +1851,15 @@ _PyMem_DebugRawAlloc(int use_calloc, void *ctx, size_t nbytes)
18611851
return NULL;
18621852

18631853
if (use_calloc)
1864-
p = (uchar *)api->alloc.calloc(api->alloc.ctx, 1, total);
1854+
p = (uint8_t *)api->alloc.calloc(api->alloc.ctx, 1, total);
18651855
else
1866-
p = (uchar *)api->alloc.malloc(api->alloc.ctx, total);
1856+
p = (uint8_t *)api->alloc.malloc(api->alloc.ctx, total);
18671857
if (p == NULL)
18681858
return NULL;
18691859

18701860
/* at p, write size (SST bytes), id (1 byte), pad (SST-1 bytes) */
18711861
write_size_t(p, nbytes);
1872-
p[SST] = (uchar)api->api_id;
1862+
p[SST] = (uint8_t)api->api_id;
18731863
memset(p + SST + 1, FORBIDDENBYTE, SST-1);
18741864

18751865
if (nbytes > 0 && !use_calloc)
@@ -1907,7 +1897,7 @@ static void
19071897
_PyMem_DebugRawFree(void *ctx, void *p)
19081898
{
19091899
debug_alloc_api_t *api = (debug_alloc_api_t *)ctx;
1910-
uchar *q = (uchar *)p - 2*SST; /* address returned from malloc */
1900+
uint8_t *q = (uint8_t *)p - 2*SST; /* address returned from malloc */
19111901
size_t nbytes;
19121902

19131903
if (p == NULL)
@@ -1924,8 +1914,8 @@ static void *
19241914
_PyMem_DebugRawRealloc(void *ctx, void *p, size_t nbytes)
19251915
{
19261916
debug_alloc_api_t *api = (debug_alloc_api_t *)ctx;
1927-
uchar *q = (uchar *)p, *oldq;
1928-
uchar *tail;
1917+
uint8_t *q = (uint8_t *)p, *oldq;
1918+
uint8_t *tail;
19291919
size_t total; /* nbytes + 4*SST */
19301920
size_t original_nbytes;
19311921
int i;
@@ -1946,7 +1936,7 @@ _PyMem_DebugRawRealloc(void *ctx, void *p, size_t nbytes)
19461936
* but we live with that.
19471937
*/
19481938
oldq = q;
1949-
q = (uchar *)api->alloc.realloc(api->alloc.ctx, q - 2*SST, total);
1939+
q = (uint8_t *)api->alloc.realloc(api->alloc.ctx, q - 2*SST, total);
19501940
if (q == NULL)
19511941
return NULL;
19521942

@@ -1956,7 +1946,7 @@ _PyMem_DebugRawRealloc(void *ctx, void *p, size_t nbytes)
19561946
}
19571947

19581948
write_size_t(q, nbytes);
1959-
assert(q[SST] == (uchar)api->api_id);
1949+
assert(q[SST] == (uint8_t)api->api_id);
19601950
for (i = 1; i < SST; ++i)
19611951
assert(q[SST + i] == FORBIDDENBYTE);
19621952
q += 2*SST;
@@ -2020,11 +2010,11 @@ _PyMem_DebugRealloc(void *ctx, void *ptr, size_t nbytes)
20202010
static void
20212011
_PyMem_DebugCheckAddress(char api, const void *p)
20222012
{
2023-
const uchar *q = (const uchar *)p;
2013+
const uint8_t *q = (const uint8_t *)p;
20242014
char msgbuf[64];
20252015
char *msg;
20262016
size_t nbytes;
2027-
const uchar *tail;
2017+
const uint8_t *tail;
20282018
int i;
20292019
char id;
20302020

@@ -2073,8 +2063,8 @@ _PyMem_DebugCheckAddress(char api, const void *p)
20732063
static void
20742064
_PyObject_DebugDumpAddress(const void *p)
20752065
{
2076-
const uchar *q = (const uchar *)p;
2077-
const uchar *tail;
2066+
const uint8_t *q = (const uint8_t *)p;
2067+
const uint8_t *tail;
20782068
size_t nbytes, serial;
20792069
int i;
20802070
int ok;
@@ -2107,7 +2097,7 @@ _PyObject_DebugDumpAddress(const void *p)
21072097
fprintf(stderr, "not all FORBIDDENBYTE (0x%02x):\n",
21082098
FORBIDDENBYTE);
21092099
for (i = SST-1; i >= 1; --i) {
2110-
const uchar byte = *(q-i);
2100+
const uint8_t byte = *(q-i);
21112101
fprintf(stderr, " at p-%d: 0x%02x", i, byte);
21122102
if (byte != FORBIDDENBYTE)
21132103
fputs(" *** OUCH", stderr);
@@ -2135,7 +2125,7 @@ _PyObject_DebugDumpAddress(const void *p)
21352125
fprintf(stderr, "not all FORBIDDENBYTE (0x%02x):\n",
21362126
FORBIDDENBYTE);
21372127
for (i = 0; i < SST; ++i) {
2138-
const uchar byte = tail[i];
2128+
const uint8_t byte = tail[i];
21392129
fprintf(stderr, " at tail+%d: 0x%02x",
21402130
i, byte);
21412131
if (byte != FORBIDDENBYTE)
@@ -2297,27 +2287,26 @@ _PyObject_DebugMallocStats(FILE *out)
22972287
*/
22982288
for (i = 0; i < maxarenas; ++i) {
22992289
uint j;
2300-
uptr base = arenas[i].address;
2290+
uinptr_t base = arenas[i].address;
23012291

23022292
/* Skip arenas which are not allocated. */
2303-
if (arenas[i].address == (uptr)NULL)
2293+
if (arenas[i].address == (uinptr_t)NULL)
23042294
continue;
23052295
narenas += 1;
23062296

23072297
numfreepools += arenas[i].nfreepools;
23082298

23092299
/* round up to pool alignment */
2310-
if (base & (uptr)POOL_SIZE_MASK) {
2300+
if (base & (uinptr_t)POOL_SIZE_MASK) {
23112301
arena_alignment += POOL_SIZE;
2312-
base &= ~(uptr)POOL_SIZE_MASK;
2302+
base &= ~(uinptr_t)POOL_SIZE_MASK;
23132303
base += POOL_SIZE;
23142304
}
23152305

23162306
/* visit every pool in the arena */
2317-
assert(base <= (uptr) arenas[i].pool_address);
2318-
for (j = 0;
2319-
base < (uptr) arenas[i].pool_address;
2320-
++j, base += POOL_SIZE) {
2307+
assert(base <= (uinptr_t) arenas[i].pool_address);
2308+
for (j = 0; base < (uinptr_t) arenas[i].pool_address;
2309+
++j, base += POOL_SIZE) {
23212310
poolp p = (poolp)base;
23222311
const uint sz = p->szidx;
23232312
uint freeblocks;

0 commit comments

Comments
 (0)