xref: /aosp_15_r20/external/pthreadpool/src/memory.c (revision b095b0533730c2930f947df924a4486d266faa1a)
1*b095b053SXin Li /* Standard C headers */
2*b095b053SXin Li #include <assert.h>
3*b095b053SXin Li #include <stddef.h>
4*b095b053SXin Li #include <stdlib.h>
5*b095b053SXin Li #include <string.h>
6*b095b053SXin Li 
7*b095b053SXin Li /* POSIX headers */
8*b095b053SXin Li #ifdef __ANDROID__
9*b095b053SXin Li 	#include <malloc.h>
10*b095b053SXin Li #endif
11*b095b053SXin Li 
12*b095b053SXin Li /* Windows headers */
13*b095b053SXin Li #ifdef _WIN32
14*b095b053SXin Li 	#include <malloc.h>
15*b095b053SXin Li #endif
16*b095b053SXin Li 
17*b095b053SXin Li /* Internal library headers */
18*b095b053SXin Li #include "threadpool-common.h"
19*b095b053SXin Li #include "threadpool-object.h"
20*b095b053SXin Li 
21*b095b053SXin Li 
pthreadpool_allocate(size_t threads_count)22*b095b053SXin Li PTHREADPOOL_INTERNAL struct pthreadpool* pthreadpool_allocate(
23*b095b053SXin Li 	size_t threads_count)
24*b095b053SXin Li {
25*b095b053SXin Li 	assert(threads_count >= 1);
26*b095b053SXin Li 
27*b095b053SXin Li 	const size_t threadpool_size = sizeof(struct pthreadpool) + threads_count * sizeof(struct thread_info);
28*b095b053SXin Li 	struct pthreadpool* threadpool = NULL;
29*b095b053SXin Li 	#if defined(__ANDROID__)
30*b095b053SXin Li 		/*
31*b095b053SXin Li 		 * Android didn't get posix_memalign until API level 17 (Android 4.2).
32*b095b053SXin Li 		 * Use (otherwise obsolete) memalign function on Android platform.
33*b095b053SXin Li 		 */
34*b095b053SXin Li 		threadpool = memalign(PTHREADPOOL_CACHELINE_SIZE, threadpool_size);
35*b095b053SXin Li 		if (threadpool == NULL) {
36*b095b053SXin Li 			return NULL;
37*b095b053SXin Li 		}
38*b095b053SXin Li 	#elif defined(_WIN32)
39*b095b053SXin Li 		threadpool = _aligned_malloc(threadpool_size, PTHREADPOOL_CACHELINE_SIZE);
40*b095b053SXin Li 		if (threadpool == NULL) {
41*b095b053SXin Li 			return NULL;
42*b095b053SXin Li 		}
43*b095b053SXin Li 	#else
44*b095b053SXin Li 		if (posix_memalign((void**) &threadpool, PTHREADPOOL_CACHELINE_SIZE, threadpool_size) != 0) {
45*b095b053SXin Li 			return NULL;
46*b095b053SXin Li 		}
47*b095b053SXin Li 	#endif
48*b095b053SXin Li 	memset(threadpool, 0, threadpool_size);
49*b095b053SXin Li 	return threadpool;
50*b095b053SXin Li }
51*b095b053SXin Li 
52*b095b053SXin Li 
pthreadpool_deallocate(struct pthreadpool * threadpool)53*b095b053SXin Li PTHREADPOOL_INTERNAL void pthreadpool_deallocate(
54*b095b053SXin Li 	struct pthreadpool* threadpool)
55*b095b053SXin Li {
56*b095b053SXin Li 	assert(threadpool != NULL);
57*b095b053SXin Li 
58*b095b053SXin Li 	const size_t threadpool_size = sizeof(struct pthreadpool) + threadpool->threads_count.value * sizeof(struct thread_info);
59*b095b053SXin Li 	memset(threadpool, 0, threadpool_size);
60*b095b053SXin Li 
61*b095b053SXin Li 	#ifdef _WIN32
62*b095b053SXin Li 		_aligned_free(threadpool);
63*b095b053SXin Li 	#else
64*b095b053SXin Li 		free(threadpool);
65*b095b053SXin Li 	#endif
66*b095b053SXin Li }
67