Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 1 | /* |
| 2 | * shctx.c - shared context management functions for SSL |
| 3 | * |
| 4 | * Copyright (C) 2011-2012 EXCELIANCE |
| 5 | * |
| 6 | * Author: Emeric Brun - emeric@exceliance.fr |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * as published by the Free Software Foundation; either version |
| 11 | * 2 of the License, or (at your option) any later version. |
| 12 | */ |
| 13 | |
| 14 | #include <sys/mman.h> |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 15 | #include <arpa/inet.h> |
Willy Tarreau | 8d2b777 | 2020-05-27 10:58:19 +0200 | [diff] [blame] | 16 | #include <import/ebmbtree.h> |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 17 | #include <types/global.h> |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 18 | #include <common/mini-clist.h> |
| 19 | #include "proto/shctx.h" |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 20 | |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 21 | #if !defined (USE_PRIVATE_CACHE) |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 22 | |
William Lallemand | 24a7a75 | 2017-10-09 14:17:39 +0200 | [diff] [blame] | 23 | int use_shared_mem = 0; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 24 | |
Emeric Brun | 9faf071 | 2012-09-25 11:11:16 +0200 | [diff] [blame] | 25 | #endif |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 26 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 27 | /* |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 28 | * Reserve a new row if <first> is null, put it in the hotlist, set the refcount to 1 |
| 29 | * or append new blocks to the row with <first> as first block if non null. |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 30 | * |
| 31 | * Reserve blocks in the avail list and put them in the hot list |
| 32 | * Return the first block put in the hot list or NULL if not enough blocks available |
| 33 | */ |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 34 | struct shared_block *shctx_row_reserve_hot(struct shared_context *shctx, |
| 35 | struct shared_block *first, int data_len) |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 36 | { |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 37 | struct shared_block *last = NULL, *block, *sblock, *ret = NULL, *next; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 38 | int enough = 0; |
| 39 | int freed = 0; |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 40 | int remain; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 41 | |
| 42 | /* not enough usable blocks */ |
| 43 | if (data_len > shctx->nbav * shctx->block_size) |
| 44 | goto out; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 45 | |
Frédéric Lécaille | b7838af | 2018-10-22 16:21:39 +0200 | [diff] [blame] | 46 | /* Check the object size limit. */ |
| 47 | if (shctx->max_obj_size > 0) { |
| 48 | if ((first && first->len + data_len > shctx->max_obj_size) || |
| 49 | (!first && data_len > shctx->max_obj_size)) |
| 50 | goto out; |
| 51 | } |
| 52 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 53 | /* Note that <remain> is nul only if <first> is not nul. */ |
| 54 | remain = 1; |
| 55 | if (first) { |
| 56 | /* Check that there is some block to reserve. |
| 57 | * In this first block of code we compute the remaining room in the |
| 58 | * current list of block already reserved for this object. |
| 59 | * We return asap if there is enough room to copy <data_len> bytes. |
| 60 | */ |
| 61 | last = first->last_reserved; |
| 62 | /* Remaining room. */ |
| 63 | remain = (shctx->block_size * first->block_count - first->len); |
| 64 | if (remain) { |
| 65 | if (remain > data_len) { |
| 66 | return last ? last : first; |
| 67 | } else { |
| 68 | data_len -= remain; |
| 69 | if (!data_len) |
| 70 | return last ? last : first; |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 75 | while (!enough && !LIST_ISEMPTY(&shctx->avail)) { |
| 76 | int count = 0; |
| 77 | int first_count = 0, first_len = 0; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 78 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 79 | next = block = LIST_NEXT(&shctx->avail, struct shared_block *, list); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 80 | if (ret == NULL) |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 81 | ret = next; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 82 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 83 | first_count = next->block_count; |
| 84 | first_len = next->len; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 85 | /* |
| 86 | Should never been set to 0. |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 87 | if (next->block_count == 0) |
| 88 | next->block_count = 1; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 89 | */ |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 90 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 91 | list_for_each_entry_safe_from(block, sblock, &shctx->avail, list) { |
| 92 | |
| 93 | /* release callback */ |
| 94 | if (first_len && shctx->free_block) |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 95 | shctx->free_block(next, block); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 96 | |
| 97 | block->block_count = 1; |
| 98 | block->len = 0; |
| 99 | |
| 100 | freed++; |
| 101 | data_len -= shctx->block_size; |
| 102 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 103 | if (data_len > 0 || !enough) { |
| 104 | if (last) { |
| 105 | shctx_block_append_hot(shctx, &last->list, block); |
| 106 | last = block; |
| 107 | } else { |
| 108 | shctx_block_set_hot(shctx, block); |
| 109 | } |
| 110 | if (!remain) { |
| 111 | first->last_append = block; |
| 112 | remain = 1; |
| 113 | } |
| 114 | if (data_len <= 0) { |
| 115 | ret->block_count = freed; |
| 116 | ret->refcount = 1; |
| 117 | ret->last_reserved = block; |
| 118 | enough = 1; |
| 119 | } |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 120 | } |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 121 | count++; |
| 122 | if (count >= first_count) |
| 123 | break; |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 124 | } |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 125 | } |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 126 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 127 | if (first) { |
| 128 | first->block_count += ret->block_count; |
| 129 | first->last_reserved = ret->last_reserved; |
| 130 | /* Reset this block. */ |
| 131 | ret->last_reserved = NULL; |
| 132 | ret->block_count = 1; |
| 133 | ret->refcount = 0; |
| 134 | /* Return the first block. */ |
| 135 | ret = first; |
| 136 | } |
| 137 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 138 | out: |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 139 | return ret; |
| 140 | } |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 141 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 142 | /* |
| 143 | * if the refcount is 0 move the row to the hot list. Increment the refcount |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 144 | */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 145 | void shctx_row_inc_hot(struct shared_context *shctx, struct shared_block *first) |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 146 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 147 | struct shared_block *block, *sblock; |
| 148 | int count = 0; |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 149 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 150 | if (first->refcount <= 0) { |
| 151 | |
| 152 | block = first; |
| 153 | |
| 154 | list_for_each_entry_safe_from(block, sblock, &shctx->avail, list) { |
| 155 | |
| 156 | shctx_block_set_hot(shctx, block); |
| 157 | |
| 158 | count++; |
| 159 | if (count >= first->block_count) |
| 160 | break; |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 161 | } |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 162 | } |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 163 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 164 | first->refcount++; |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 165 | } |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 166 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 167 | /* |
| 168 | * decrement the refcount and move the row at the end of the avail list if it reaches 0. |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 169 | */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 170 | void shctx_row_dec_hot(struct shared_context *shctx, struct shared_block *first) |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 171 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 172 | struct shared_block *block, *sblock; |
| 173 | int count = 0; |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 174 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 175 | first->refcount--; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 176 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 177 | if (first->refcount <= 0) { |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 178 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 179 | block = first; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 180 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 181 | list_for_each_entry_safe_from(block, sblock, &shctx->hot, list) { |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 182 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 183 | shctx_block_set_avail(shctx, block); |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 184 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 185 | count++; |
| 186 | if (count >= first->block_count) |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 187 | break; |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 188 | } |
| 189 | } |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 190 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | |
| 194 | /* |
| 195 | * Append data in the row if there is enough space. |
| 196 | * The row should be in the hot list |
| 197 | * |
| 198 | * Return the amount of appended data if ret >= 0 |
| 199 | * or how much more space it needs to contains the data if < 0. |
| 200 | */ |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 201 | int shctx_row_data_append(struct shared_context *shctx, |
| 202 | struct shared_block *first, struct shared_block *from, |
| 203 | unsigned char *data, int len) |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 204 | { |
| 205 | int remain, start; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 206 | struct shared_block *block; |
| 207 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 208 | /* return -<len> needed to work */ |
| 209 | if (len > first->block_count * shctx->block_size - first->len) |
| 210 | return (first->block_count * shctx->block_size - first->len) - len; |
| 211 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 212 | block = from ? from : first; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 213 | list_for_each_entry_from(block, &shctx->hot, list) { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 214 | /* end of copy */ |
| 215 | if (len <= 0) |
| 216 | break; |
| 217 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 218 | /* remaining written bytes in the current block. */ |
| 219 | remain = (shctx->block_size * first->block_count - first->len) % shctx->block_size; |
| 220 | /* if remain == 0, previous buffers are full, or first->len == 0 */ |
| 221 | if (!remain) { |
| 222 | remain = shctx->block_size; |
| 223 | start = 0; |
| 224 | } |
| 225 | else { |
| 226 | /* start must be calculated before remain is modified */ |
| 227 | start = shctx->block_size - remain; |
| 228 | } |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 229 | |
| 230 | /* must not try to copy more than len */ |
| 231 | remain = MIN(remain, len); |
| 232 | |
| 233 | memcpy(block->data + start, data, remain); |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 234 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 235 | data += remain; |
| 236 | len -= remain; |
| 237 | first->len += remain; /* update len in the head of the row */ |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 238 | first->last_append = block; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | return len; |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 242 | } |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 243 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 244 | /* |
| 245 | * Copy <len> data from a row of blocks, return the remaining data to copy |
Joseph Herlant | 3952643 | 2018-11-25 11:31:31 -0800 | [diff] [blame] | 246 | * If 0 is returned, the full data has successfully been copied |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 247 | * |
| 248 | * The row should be in the hot list |
| 249 | */ |
| 250 | int shctx_row_data_get(struct shared_context *shctx, struct shared_block *first, |
| 251 | unsigned char *dst, int offset, int len) |
| 252 | { |
| 253 | int count = 0, size = 0, start = -1; |
| 254 | struct shared_block *block; |
| 255 | |
William Lallemand | 7217c46 | 2017-10-31 20:21:46 +0100 | [diff] [blame] | 256 | /* can't copy more */ |
| 257 | if (len > first->len) |
| 258 | len = first->len; |
| 259 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 260 | block = first; |
| 261 | count = 0; |
| 262 | /* Pass through the blocks to copy them */ |
| 263 | list_for_each_entry_from(block, &shctx->hot, list) { |
| 264 | if (count >= first->block_count || len <= 0) |
| 265 | break; |
| 266 | |
| 267 | count++; |
| 268 | /* continue until we are in right block |
| 269 | corresponding to the offset */ |
| 270 | if (count < offset / shctx->block_size + 1) |
| 271 | continue; |
| 272 | |
| 273 | /* on the first block, data won't possibly began at offset 0 */ |
| 274 | if (start == -1) |
| 275 | start = offset - (count - 1) * shctx->block_size; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 276 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 277 | /* size can be lower than a block when copying the last block */ |
| 278 | size = MIN(shctx->block_size - start, len); |
| 279 | |
| 280 | memcpy(dst, block->data + start, size); |
| 281 | dst += size; |
| 282 | len -= size; |
| 283 | start = 0; |
| 284 | } |
| 285 | return len; |
| 286 | } |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 287 | |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 288 | /* Allocate shared memory context. |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 289 | * <maxblocks> is maximum blocks. |
| 290 | * If <maxblocks> is set to less or equal to 0, ssl cache is disabled. |
| 291 | * Returns: -1 on alloc failure, <maxblocks> if it performs context alloc, |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 292 | * and 0 if cache is already allocated. |
| 293 | */ |
Frédéric Lécaille | b7838af | 2018-10-22 16:21:39 +0200 | [diff] [blame] | 294 | int shctx_init(struct shared_context **orig_shctx, int maxblocks, int blocksize, |
Frédéric Lécaille | b80bc27 | 2018-10-25 20:31:40 +0200 | [diff] [blame] | 295 | unsigned int maxobjsz, int extra, int shared) |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 296 | { |
| 297 | int i; |
William Lallemand | 3f85c9a | 2017-10-09 16:30:50 +0200 | [diff] [blame] | 298 | struct shared_context *shctx; |
| 299 | int ret; |
Emeric Brun | 9faf071 | 2012-09-25 11:11:16 +0200 | [diff] [blame] | 300 | #ifndef USE_PRIVATE_CACHE |
Emeric Brun | cd1a526 | 2014-05-07 23:11:42 +0200 | [diff] [blame] | 301 | #ifdef USE_PTHREAD_PSHARED |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 302 | pthread_mutexattr_t attr; |
Emeric Brun | cd1a526 | 2014-05-07 23:11:42 +0200 | [diff] [blame] | 303 | #endif |
Emeric Brun | 9faf071 | 2012-09-25 11:11:16 +0200 | [diff] [blame] | 304 | #endif |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 305 | void *cur; |
Emeric Brun | 4b3091e | 2012-09-24 15:48:52 +0200 | [diff] [blame] | 306 | int maptype = MAP_PRIVATE; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 307 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 308 | if (maxblocks <= 0) |
Emeric Brun | 22890a1 | 2012-12-28 14:41:32 +0100 | [diff] [blame] | 309 | return 0; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 310 | |
Willy Tarreau | a7ddab0 | 2020-02-21 13:45:58 +0100 | [diff] [blame] | 311 | /* make sure to align the records on a pointer size */ |
| 312 | blocksize = (blocksize + sizeof(void *) - 1) & -sizeof(void *); |
| 313 | extra = (extra + sizeof(void *) - 1) & -sizeof(void *); |
| 314 | |
Emeric Brun | 9faf071 | 2012-09-25 11:11:16 +0200 | [diff] [blame] | 315 | #ifndef USE_PRIVATE_CACHE |
Emeric Brun | 4b3091e | 2012-09-24 15:48:52 +0200 | [diff] [blame] | 316 | if (shared) |
| 317 | maptype = MAP_SHARED; |
Emeric Brun | 9faf071 | 2012-09-25 11:11:16 +0200 | [diff] [blame] | 318 | #endif |
Emeric Brun | 4b3091e | 2012-09-24 15:48:52 +0200 | [diff] [blame] | 319 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 320 | shctx = (struct shared_context *)mmap(NULL, sizeof(struct shared_context) + extra + (maxblocks * (sizeof(struct shared_block) + blocksize)), |
Emeric Brun | 4b3091e | 2012-09-24 15:48:52 +0200 | [diff] [blame] | 321 | PROT_READ | PROT_WRITE, maptype | MAP_ANON, -1, 0); |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 322 | if (!shctx || shctx == MAP_FAILED) { |
| 323 | shctx = NULL; |
William Lallemand | 3f85c9a | 2017-10-09 16:30:50 +0200 | [diff] [blame] | 324 | ret = SHCTX_E_ALLOC_CACHE; |
| 325 | goto err; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 326 | } |
| 327 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 328 | shctx->nbav = 0; |
| 329 | |
Emeric Brun | 9faf071 | 2012-09-25 11:11:16 +0200 | [diff] [blame] | 330 | #ifndef USE_PRIVATE_CACHE |
Emeric Brun | caa19cc | 2014-05-07 16:10:18 +0200 | [diff] [blame] | 331 | if (maptype == MAP_SHARED) { |
Emeric Brun | cd1a526 | 2014-05-07 23:11:42 +0200 | [diff] [blame] | 332 | #ifdef USE_PTHREAD_PSHARED |
Emeric Brun | caa19cc | 2014-05-07 16:10:18 +0200 | [diff] [blame] | 333 | if (pthread_mutexattr_init(&attr)) { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 334 | munmap(shctx, sizeof(struct shared_context) + extra + (maxblocks * (sizeof(struct shared_block) + blocksize))); |
Emeric Brun | caa19cc | 2014-05-07 16:10:18 +0200 | [diff] [blame] | 335 | shctx = NULL; |
William Lallemand | 3f85c9a | 2017-10-09 16:30:50 +0200 | [diff] [blame] | 336 | ret = SHCTX_E_INIT_LOCK; |
| 337 | goto err; |
Emeric Brun | caa19cc | 2014-05-07 16:10:18 +0200 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | if (pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED)) { |
| 341 | pthread_mutexattr_destroy(&attr); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 342 | munmap(shctx, sizeof(struct shared_context) + extra + (maxblocks * (sizeof(struct shared_block) + blocksize))); |
Emeric Brun | caa19cc | 2014-05-07 16:10:18 +0200 | [diff] [blame] | 343 | shctx = NULL; |
William Lallemand | 3f85c9a | 2017-10-09 16:30:50 +0200 | [diff] [blame] | 344 | ret = SHCTX_E_INIT_LOCK; |
| 345 | goto err; |
Emeric Brun | caa19cc | 2014-05-07 16:10:18 +0200 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | if (pthread_mutex_init(&shctx->mutex, &attr)) { |
| 349 | pthread_mutexattr_destroy(&attr); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 350 | munmap(shctx, sizeof(struct shared_context) + extra + (maxblocks * (sizeof(struct shared_block) + blocksize))); |
Emeric Brun | caa19cc | 2014-05-07 16:10:18 +0200 | [diff] [blame] | 351 | shctx = NULL; |
William Lallemand | 3f85c9a | 2017-10-09 16:30:50 +0200 | [diff] [blame] | 352 | ret = SHCTX_E_INIT_LOCK; |
| 353 | goto err; |
Emeric Brun | caa19cc | 2014-05-07 16:10:18 +0200 | [diff] [blame] | 354 | } |
Emeric Brun | cd1a526 | 2014-05-07 23:11:42 +0200 | [diff] [blame] | 355 | #else |
| 356 | shctx->waiters = 0; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 357 | #endif |
Emeric Brun | 4b3091e | 2012-09-24 15:48:52 +0200 | [diff] [blame] | 358 | use_shared_mem = 1; |
Emeric Brun | caa19cc | 2014-05-07 16:10:18 +0200 | [diff] [blame] | 359 | } |
Emeric Brun | 9faf071 | 2012-09-25 11:11:16 +0200 | [diff] [blame] | 360 | #endif |
Emeric Brun | 4b3091e | 2012-09-24 15:48:52 +0200 | [diff] [blame] | 361 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 362 | LIST_INIT(&shctx->avail); |
| 363 | LIST_INIT(&shctx->hot); |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 364 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 365 | shctx->block_size = blocksize; |
Frédéric Lécaille | b80bc27 | 2018-10-25 20:31:40 +0200 | [diff] [blame] | 366 | shctx->max_obj_size = maxobjsz == (unsigned int)-1 ? 0 : maxobjsz; |
Emeric Brun | af9619d | 2012-11-28 18:47:52 +0100 | [diff] [blame] | 367 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 368 | /* init the free blocks after the shared context struct */ |
| 369 | cur = (void *)shctx + sizeof(struct shared_context) + extra; |
| 370 | for (i = 0; i < maxblocks; i++) { |
| 371 | struct shared_block *cur_block = (struct shared_block *)cur; |
| 372 | cur_block->len = 0; |
| 373 | cur_block->refcount = 0; |
| 374 | cur_block->block_count = 1; |
| 375 | LIST_ADDQ(&shctx->avail, &cur_block->list); |
| 376 | shctx->nbav++; |
| 377 | cur += sizeof(struct shared_block) + blocksize; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 378 | } |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 379 | ret = maxblocks; |
William Lallemand | 3f85c9a | 2017-10-09 16:30:50 +0200 | [diff] [blame] | 380 | |
| 381 | err: |
| 382 | *orig_shctx = shctx; |
| 383 | return ret; |
Emeric Brun | 3e541d1 | 2012-09-03 11:14:36 +0200 | [diff] [blame] | 384 | } |
| 385 | |