Skip to content

Commit 45eda1d

Browse files
committed
optimized calloc zero-filling
1 parent 1c69db9 commit 45eda1d

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

src/libc/allocator.src

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,33 @@
77
_calloc:
88
pop de
99
pop bc
10-
ex (sp),hl
10+
ex (sp), hl
1111
push bc
1212
push de
1313
call __imulu
1414
push hl
1515
push hl
1616
call _malloc
17-
pop de
18-
add hl,de
19-
xor a,a
20-
sbc hl,de
21-
ld e,a
22-
push de
17+
pop bc ; reset SP
18+
; test for NULL
19+
add hl, bc
20+
or a, a
21+
sbc hl, bc
22+
pop bc ; BC = size
23+
ret z ; return NULL
24+
; inlined bzero
2325
push hl
24-
call nz,_memset
25-
pop de
26-
pop de
27-
pop de
26+
ex de, hl ; DE = dest
27+
; test if the size is zero
28+
scf
29+
sbc hl, hl
30+
add hl, bc
31+
jr nc, .finish
32+
; large region of all zeros on the Ti84CE
33+
ld hl, $E40000 ; HL = src
34+
ldir
35+
.finish:
36+
pop hl ; return value
2837
ret
2938

3039
if defined ALLOCATOR_SIMPLE

test/standalone/asprintf_fprintf/src/main.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,12 @@ int memccpy_tests(void) {
415415
return __LINE__;
416416
}
417417

418+
/* check that no crashes occur with small calloc sizes */
419+
buf = (char*)calloc(1, sizeof(char));
420+
free(buf);
421+
buf = (char*)calloc(0, sizeof(char));
422+
free(buf);
423+
418424
buf = (char*)calloc(file_size + 1, sizeof(char));
419425
if (buf == NULL) {
420426
perror("calloc failure");

0 commit comments

Comments
 (0)