Warning: That file was not part of the compilation database. It may have many parsing errors.

1#include <errno.h>
2#include <limits.h>
3#include <pthread.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7#include <unistd.h>
8#include <sys/mman.h>
9#include <sys/resource.h>
10
11static void use_stack (size_t needed);
12
13void (*use_stack_ptr) (size_t) = use_stack;
14
15static void
16use_stack (size_t needed)
17{
18 size_t sz = sysconf (_SC_PAGESIZE);
19 char *buf = alloca (sz);
20 memset (buf, '\0', sz);
21
22 if (needed > sz)
23 use_stack_ptr (needed - sz);
24}
25
26static void
27use_up_memory (void)
28{
29 struct rlimit rl;
30 getrlimit (RLIMIT_AS, &rl);
31 rl.rlim_cur = 10 * 1024 * 1024;
32 setrlimit (RLIMIT_AS, &rl);
33
34 char *c;
35 int PAGESIZE = getpagesize ();
36 while (1)
37 {
38 c = mmap (NULL, PAGESIZE, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0);
39 if (c == MAP_FAILED)
40 break;
41 }
42}
43
44static void *
45child (void *arg)
46{
47 sleep (1);
48 return arg;
49}
50
51static int
52do_test (void)
53{
54 int err;
55 pthread_t tid;
56
57 /* Allocate the memory needed for the stack. */
58 use_stack_ptr (PTHREAD_STACK_MIN);
59
60 use_up_memory ();
61
62 err = pthread_create (&tid, NULL, child, NULL);
63 if (err != 0)
64 {
65 printf ("pthread_create returns %d: %s\n", err,
66 err == EAGAIN ? "OK" : "FAIL");
67 return err != EAGAIN;
68 }
69
70 /* We did not fail to allocate memory despite the preparation. Oh well. */
71 return 0;
72}
73
74#define TEST_FUNCTION do_test ()
75#include "../test-skeleton.c"
76

Warning: That file was not part of the compilation database. It may have many parsing errors.