Browse code

tls_wolfssl: reduce overhead per fragment to maintain 16 byte alignment

S-P Chan authored on 23/06/2022 02:22:31
Showing 1 changed files
... ...
@@ -222,32 +222,28 @@ static const int MAX_ALIGN = __alignof__(max_align_t);
222 222
 
223 223
 static void* ser_malloc(size_t size)
224 224
 {
225
-	char* ptr =  shm_malloc(size + 2*MAX_ALIGN);
226
-	int pad = MAX_ALIGN - ((long) ptr % MAX_ALIGN);
225
+	char* ptr =  shm_malloc(size + MAX_ALIGN);
226
+	int pad = MAX_ALIGN - ((long) ptr % MAX_ALIGN); // 8 or 16 bytes
227 227
 
228
-	*(size_t*)ptr = size;
229
-
230
-	memset(ptr + MAX_ALIGN, pad, pad);
231
-	return ptr + MAX_ALIGN + pad;
228
+	memset(ptr, pad, pad);
229
+	return ptr + pad;
232 230
 }
233 231
 
234 232
 static void* ser_realloc(void *ptr, size_t new_size)
235 233
 {
236 234
 	if(!ptr) return ser_malloc(new_size);
237 235
 
238
-	int pad = *((unsigned char*)ptr - 1);
239
-	unsigned char *real_ptr = (unsigned char*)ptr - pad - MAX_ALIGN;
240
-	int size = *(size_t*)real_ptr;
236
+	int pad = *((char*)ptr - 1); // 8 or 16 bytes
237
+	char *real_ptr = (char*)ptr - pad; 
241 238
 
242
-	char *new_ptr = shm_realloc(real_ptr, new_size+2*MAX_ALIGN);
243
-	*(size_t*)new_ptr = new_size;
239
+	char *new_ptr = shm_realloc(real_ptr, new_size+MAX_ALIGN);
244 240
 	int new_pad = MAX_ALIGN - ((long) new_ptr % MAX_ALIGN);
245 241
 	if (new_pad != pad) {
246
-		memmove(new_ptr + MAX_ALIGN + new_pad, new_ptr + MAX_ALIGN + pad, new_size);
247
-		memset(new_ptr + MAX_ALIGN, new_pad, new_pad);
242
+		memmove(new_ptr + new_pad, new_ptr + pad, new_size);
243
+		memset(new_ptr, new_pad, new_pad);
248 244
 	}
249 245
 		
250
-	return new_ptr + MAX_ALIGN + new_pad;
246
+	return new_ptr + new_pad;
251 247
 }
252 248
 #endif /* LIBRESSL_VERSION_NUMBER */
253 249
 
... ...
@@ -255,7 +251,7 @@ static void ser_free(void *ptr)
255 251
 {
256 252
 	if (ptr) {
257 253
 		int pad = *((unsigned char *)ptr - 1);
258
-		shm_free((unsigned char*)ptr - pad  - MAX_ALIGN);
254
+		shm_free((unsigned char*)ptr - pad);
259 255
 	}
260 256
 }
261 257