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