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