summaryrefslogtreecommitdiff
path: root/src/regress/lib/libc/malloc/malloc_ulimit2/malloc_ulimit2.c
blob: 5d9e3774e71b1fb36c41e82e2f5f2b19409df10b (plain)
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
/*	$OpenBSD: malloc_ulimit2.c,v 1.5 2019/06/12 11:31:36 bluhm Exp $	*/

/* Public Domain, 2006, Otto Moerbeek <otto@drijf.net> */

#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <err.h>
#include <stdlib.h>
#include <stdio.h>

#define FACTOR	1024

/* This test takes forever with junking turned on. */
char *malloc_options = "jj";

int
main()
{
	struct rlimit lim;
	size_t sz;
	int i;
	void *p;

	if (getrlimit(RLIMIT_DATA, &lim) == -1)
		err(1, "getrlimit");

	sz = lim.rlim_cur / FACTOR;

	for (i = 0; ; i++) {
		size_t len = (sz-i) * FACTOR;
		p = malloc(len);
		if (p != NULL) {
			free(p);
			break;
		}
	}
	i += 10;
	for (; i >= 0; i--) {
		size_t len = (sz-i) * FACTOR;
		p = malloc(len);
		free(p);
		free(malloc(4096));
	}
	return (0);
}