Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] authored and Kevin Newton committed Jan 31, 2025
1 parent 9b130e6 commit cff2141
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
25 changes: 13 additions & 12 deletions src/libImaging/Imaging.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,17 @@ extern "C" {
*/

#ifdef Py_GIL_DISABLED
#if defined(__cplusplus)
#define IMAGING_TLS thread_local
#elif defined(HAVE_THREAD_LOCAL)
#define IMAGING_TLS thread_local
#elif defined(HAVE__THREAD_LOCAL)
#define IMAGING_TLS _Thread_local
#elif defined(HAVE___THREAD)
#define IMAGING_TLS __thread
#elif defined(HAVE___DECLSPEC_THREAD_)
#define IMAGING_TLS __declspec(thread)
#endif
#if defined(__cplusplus)
#define IMAGING_TLS thread_local
#elif defined(HAVE_THREAD_LOCAL)
#define IMAGING_TLS thread_local
#elif defined(HAVE__THREAD_LOCAL)
#define IMAGING_TLS _Thread_local
#elif defined(HAVE___THREAD)
#define IMAGING_TLS __thread
#elif defined(HAVE___DECLSPEC_THREAD_)
#define IMAGING_TLS __declspec(thread)
#endif
#endif

/* Handles */
Expand Down Expand Up @@ -216,7 +216,8 @@ extern struct ImagingMemoryArena ImagingDefaultArena;
for ((arena) = &ImagingDefaultArena; (arena); (arena) = NULL)
#endif

ImagingMemoryArena ImagingGetArena(void);
ImagingMemoryArena
ImagingGetArena(void);

extern int
ImagingMemorySetBlocksMax(ImagingMemoryArena arena, int blocks_max);
Expand Down
37 changes: 20 additions & 17 deletions src/libImaging/Storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,32 +274,35 @@ static IMAGING_TLS uint64_t ImagingArenaThreadIndex = UINT64_MAX;

/* These are the statically-allocated arenas. */
struct ImagingMemoryArena ImagingArenas[IMAGING_ARENAS_COUNT] = {
{ 1, IMAGING_ARENA_BLOCK_SIZE, 0, 0, NULL, 0, 0, 0, 0, 0, 0, {0} },
{ 1, IMAGING_ARENA_BLOCK_SIZE, 0, 0, NULL, 0, 0, 0, 0, 0, 1, {0} },
{ 1, IMAGING_ARENA_BLOCK_SIZE, 0, 0, NULL, 0, 0, 0, 0, 0, 2, {0} },
{ 1, IMAGING_ARENA_BLOCK_SIZE, 0, 0, NULL, 0, 0, 0, 0, 0, 3, {0} },
{ 1, IMAGING_ARENA_BLOCK_SIZE, 0, 0, NULL, 0, 0, 0, 0, 0, 4, {0} },
{ 1, IMAGING_ARENA_BLOCK_SIZE, 0, 0, NULL, 0, 0, 0, 0, 0, 5, {0} },
{ 1, IMAGING_ARENA_BLOCK_SIZE, 0, 0, NULL, 0, 0, 0, 0, 0, 6, {0} },
{ 1, IMAGING_ARENA_BLOCK_SIZE, 0, 0, NULL, 0, 0, 0, 0, 0, 7, {0} },
{ 0 }
{1, IMAGING_ARENA_BLOCK_SIZE, 0, 0, NULL, 0, 0, 0, 0, 0, 0, {0}},
{1, IMAGING_ARENA_BLOCK_SIZE, 0, 0, NULL, 0, 0, 0, 0, 0, 1, {0}},
{1, IMAGING_ARENA_BLOCK_SIZE, 0, 0, NULL, 0, 0, 0, 0, 0, 2, {0}},
{1, IMAGING_ARENA_BLOCK_SIZE, 0, 0, NULL, 0, 0, 0, 0, 0, 3, {0}},
{1, IMAGING_ARENA_BLOCK_SIZE, 0, 0, NULL, 0, 0, 0, 0, 0, 4, {0}},
{1, IMAGING_ARENA_BLOCK_SIZE, 0, 0, NULL, 0, 0, 0, 0, 0, 5, {0}},
{1, IMAGING_ARENA_BLOCK_SIZE, 0, 0, NULL, 0, 0, 0, 0, 0, 6, {0}},
{1, IMAGING_ARENA_BLOCK_SIZE, 0, 0, NULL, 0, 0, 0, 0, 0, 7, {0}},
{0}
};

/* Get a pointer to the correct arena for this context. In this case where we
* are using a round-robin approach to the statically allocated arenas, we will
* return the arena that is assigned to the thread on first use.
*/
ImagingMemoryArena ImagingGetArena(void) {
ImagingMemoryArena
ImagingGetArena(void) {
if (ImagingArenaThreadIndex == UINT64_MAX) {
ImagingArenaThreadIndex = _Py_atomic_add_uint64(&ImagingArenaIndex, 1) % IMAGING_ARENAS_COUNT;
ImagingArenaThreadIndex =
_Py_atomic_add_uint64(&ImagingArenaIndex, 1) % IMAGING_ARENAS_COUNT;
}
return &ImagingArenas[ImagingArenaThreadIndex];
}

/* Return the arena associated with the given image. In this case the index of
* the arena is stored on the image itself.
*/
ImagingMemoryArena ImagingGetArenaFromImaging(Imaging im) {
ImagingMemoryArena
ImagingGetArenaFromImaging(Imaging im) {
int arenaindex = im->arenaindex;
assert(arenaindex >= 0 && arenaindex < IMAGING_ARENAS_COUNT);
return &ImagingArenas[arenaindex];
Expand All @@ -309,7 +312,8 @@ ImagingMemoryArena ImagingGetArenaFromImaging(Imaging im) {
* is necessary in order to return the blocks to the correct arena when the
* image is destroyed.
*/
static void ImagingSetArenaOnImaging(Imaging im, ImagingMemoryArena arena) {
static void
ImagingSetArenaOnImaging(Imaging im, ImagingMemoryArena arena) {
im->arenaindex = arena->index;
}
#else
Expand Down Expand Up @@ -340,7 +344,8 @@ struct ImagingMemoryArena ImagingDefaultArena = {
* either have the GIL or we do not have TLS, we will return only the default
* arena.
*/
ImagingMemoryArena ImagingGetArena(void) {
ImagingMemoryArena
ImagingGetArena(void) {
return &ImagingDefaultArena;
}

Expand Down Expand Up @@ -589,9 +594,7 @@ ImagingNewInternal(const char *mode, int xsize, int ysize, int dirty) {
ImagingSetArenaOnImaging(im, arena);

MUTEX_LOCK(&arena->mutex);
Imaging tmp = ImagingAllocateArray(
im, arena, dirty, arena->block_size
);
Imaging tmp = ImagingAllocateArray(im, arena, dirty, arena->block_size);
MUTEX_UNLOCK(&arena->mutex);
if (tmp) {
return im;
Expand Down

0 comments on commit cff2141

Please sign in to comment.