File: alloc.h

package info (click to toggle)
w3m 0.5.3%2Bgit20230121-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 11,520 kB
  • sloc: ansic: 59,204; sh: 4,331; perl: 4,217; javascript: 2,315; makefile: 913; cpp: 869; ruby: 776; awk: 78; sed: 16
file content (39 lines) | stat: -rw-r--r-- 896 bytes parent folder | download | duplicates (5)
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
/*
 * by Scarlett. public domain.
 * replacements for w3m's allocation macros which add overflow
 * detection and concentrate the macros in one file
 */
#ifndef W3_ALLOC_H
#define W3_ALLOC_H
#include <gc.h>
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>

static inline size_t
z_mult_no_oflow_(size_t n, size_t size)
{
	if (size != 0 && n > ULONG_MAX / size) {
		fprintf(stderr,
		    "w3m: overflow in malloc, %lu*%lu\n", (unsigned long)n, (unsigned long)size);
		exit(1);
	}
	return n * size;
}

#define New(type) \
	(GC_MALLOC(sizeof(type)))

#define NewAtom(type) \
	(GC_MALLOC_ATOMIC(sizeof(type)))

#define New_N(type, n) \
	(GC_MALLOC(z_mult_no_oflow_((n), sizeof(type))))

#define NewAtom_N(type, n) \
	(GC_MALLOC_ATOMIC(z_mult_no_oflow_((n), sizeof(type))))

#define New_Reuse(type, ptr, n) \
	(GC_REALLOC((ptr), z_mult_no_oflow_((n), sizeof(type))))

#endif /* W3_ALLOC_H */