Debian Codegraph @

regressions/ck_hp/validate/serial.c

raw
1 /*
2 * Copyright 2010-2015 Samy Al Bahra.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <errno.h>
28 #include <inttypes.h>
29 #include <pthread.h>
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <strings.h>
34 #include <unistd.h>
35 #include <sys/time.h>
36
37 #include <stdbool.h>
38 #include <stddef.h>
39 #include <ck_hp.h>
40
41 #include "../../common.h"
42
43 struct entry {
44 unsigned int value;
45 ck_hp_hazard_t hazard;
46 };
47
48 static void
49 destructor(void *pointer)
50 {
51
52 fprintf(stderr, "Free %p\n", pointer);
53 free(pointer);
54 return;
55 }
56
57 int
58 main(int argc, char *argv[])
59 {
60 ck_hp_t state;
61 ck_hp_record_t record[2];
62 void **pointers;
63 struct entry *entry, *other;
64
65 (void)argc;
66 (void)argv;
67
68 ck_hp_init(&state, 1, 1, destructor);
69
70 pointers = malloc(sizeof(void *));
71 if (pointers == NULL) {
72 ck_error("ERROR: Failed to allocate slot.\n");
73 }
74 ck_hp_register(&state, &record[0], pointers);
75 ck_hp_reclaim(&record[0]);
76
77 entry = malloc(sizeof *entry);
78 ck_hp_set(&record[0], 0, entry);
79 ck_hp_reclaim(&record[0]);
80 ck_hp_free(&record[0], &entry->hazard, entry, entry);
81 ck_hp_reclaim(&record[0]);
82 ck_hp_set(&record[0], 0, NULL);
83 ck_hp_reclaim(&record[0]);
84
85 entry = malloc(sizeof *entry);
86 ck_hp_set(&record[0], 0, entry);
87 ck_hp_reclaim(&record[0]);
88 ck_hp_free(&record[0], &entry->hazard, entry, entry);
89 ck_hp_reclaim(&record[0]);
90 ck_hp_set(&record[0], 0, NULL);
91 ck_hp_reclaim(&record[0]);
92
93 pointers = malloc(sizeof(void *));
94 if (pointers == NULL) {
95 ck_error("ERROR: Failed to allocate slot.\n");
96 }
97 ck_hp_register(&state, &record[1], pointers);
98 ck_hp_reclaim(&record[1]);
99
100 entry = malloc(sizeof *entry);
101 ck_hp_set(&record[1], 0, entry);
102 ck_hp_reclaim(&record[1]);
103 ck_hp_free(&record[1], &entry->hazard, entry, entry);
104 ck_hp_reclaim(&record[1]);
105 ck_hp_set(&record[1], 0, NULL);
106 ck_hp_reclaim(&record[1]);
107
108 printf("Allocating entry and freeing in other HP record...\n");
109 entry = malloc(sizeof *entry);
110 entry->value = 42;
111 ck_hp_set(&record[0], 0, entry);
112 ck_hp_free(&record[1], &entry->hazard, entry, entry);
113 ck_pr_store_uint(&entry->value, 1);
114
115 other = malloc(sizeof *other);
116 other->value = 24;
117 ck_hp_set(&record[1], 0, other);
118 ck_hp_free(&record[0], &other->hazard, other, other);
119 ck_pr_store_uint(&other->value, 32);
120 ck_hp_set(&record[0], 0, NULL);
121 ck_hp_reclaim(&record[1]);
122 ck_hp_set(&record[1], 0, NULL);
123 ck_hp_reclaim(&record[0]);
124 ck_hp_reclaim(&record[1]);
125
126 return 0;
127 }