Skip to content

Commit 8fc4c06

Browse files
committed
cleanup, always using realloc now, added proper error messages.
1 parent fae8d0d commit 8fc4c06

File tree

2 files changed

+29
-32
lines changed

2 files changed

+29
-32
lines changed

src_c/alphablit.c

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -595,9 +595,9 @@ pg_cached_blitcopy(SDL_Surface *src, SDL_Surface *dst,
595595
for (i = 0; i < destinations->size; i++) {
596596
CachedBlitDest *item = &destinations->sequence[i];
597597

598-
const Py_ssize_t src_pitch = item->w * sizeof(Uint32);
599-
const Py_ssize_t src_skip = src->pitch / 4;
600-
const Py_ssize_t dst_skip = dst->pitch / 4;
598+
const int src_pitch = item->w * sizeof(Uint32);
599+
const int src_skip = src->pitch / 4;
600+
const int dst_skip = dst->pitch / 4;
601601

602602
Uint32 *srcp32 = (Uint32 *)src->pixels + item->x + item->y * src_skip;
603603
Uint32 *dstp32 = item->pixels;
@@ -614,24 +614,15 @@ int
614614
SoftCachedBlitPyGame(SDL_Surface *src, SDL_Surface *dst, int blend_flags,
615615
BlitSequence *destinations)
616616
{
617-
int okay;
618-
int src_locked;
619-
int dst_locked;
620-
SDL_BlendMode src_blend;
617+
int okay = 1;
618+
int src_locked = 0, dst_locked = 0;
621619

622-
/* Everything is okay at the beginning... */
623-
okay = 1;
624-
625-
/* Lock the destination if it's in hardware */
626-
dst_locked = 0;
627620
if (SDL_MUSTLOCK(dst)) {
628621
if (SDL_LockSurface(dst) < 0)
629622
okay = 0;
630623
else
631624
dst_locked = 1;
632625
}
633-
/* Lock the source if it's in hardware */
634-
src_locked = 0;
635626
if (SDL_MUSTLOCK(src)) {
636627
if (SDL_LockSurface(src) < 0)
637628
okay = 0;
@@ -640,6 +631,7 @@ SoftCachedBlitPyGame(SDL_Surface *src, SDL_Surface *dst, int blend_flags,
640631
}
641632

642633
if (okay) {
634+
SDL_BlendMode src_blend;
643635
switch (blend_flags) {
644636
case 0:
645637
/* unhandled cases */
@@ -659,13 +651,12 @@ SoftCachedBlitPyGame(SDL_Surface *src, SDL_Surface *dst, int blend_flags,
659651
}
660652
}
661653

662-
/* We need to unlock the surfaces if they're locked */
663654
if (dst_locked)
664655
SDL_UnlockSurface(dst);
665656
if (src_locked)
666657
SDL_UnlockSurface(src);
667-
/* Blit is done! */
668-
return (okay ? 0 : -1);
658+
659+
return okay ? 0 : -1;
669660
}
670661

671662
/* --------------------------------------------------------- */

src_c/surface.c

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2147,6 +2147,8 @@ surf_blits(pgSurfaceObject *self, PyObject *args, PyObject *keywds)
21472147
#define FBLITS_ERR_CACHE_RLE_NOT_SUPPORTED 16
21482148
#define FBLITS_ERR_FLAG_NOT_SUPPORTED 17
21492149
#define FBLITS_ERR_NO_MEMORY 18
2150+
#define FBLITS_ERR_INVALID_SEQUENCE_LENGTH 19
2151+
#define FBLITS_ERR_INVALID_DESTINATION 20
21502152

21512153
int
21522154
_surf_fblits_item_check_and_blit(pgSurfaceObject *self, PyObject *item,
@@ -2245,20 +2247,10 @@ _surf_fblits_cached_item_check_and_blit(pgSurfaceObject *self, PyObject *item,
22452247
return FBLITS_ERR_CACHE_RLE_NOT_SUPPORTED;
22462248
}
22472249

2248-
/* manage destinations memory allocation or reallocation */
2250+
/* manage destinations memory */
22492251
Py_ssize_t new_size = PyList_GET_SIZE(pos_list);
2250-
if (destinations->sequence == NULL) {
2251-
destinations->sequence =
2252-
(CachedBlitDest *)malloc(new_size * sizeof(CachedBlitDest));
2253-
destinations->size = destinations->alloc_size = new_size;
2254-
if (!destinations->sequence) {
2255-
return FBLITS_ERR_NO_MEMORY;
2256-
}
2257-
}
2258-
else if (new_size > 0 && new_size <= destinations->alloc_size) {
2259-
destinations->size = new_size;
2260-
}
2261-
else if (new_size > destinations->alloc_size) {
2252+
if (new_size > destinations->alloc_size) {
2253+
/* no realloc as we don't care about the previous destinations */
22622254
destinations->sequence = (CachedBlitDest *)realloc(
22632255
destinations->sequence, new_size * sizeof(CachedBlitDest));
22642256

@@ -2267,8 +2259,13 @@ _surf_fblits_cached_item_check_and_blit(pgSurfaceObject *self, PyObject *item,
22672259

22682260
destinations->size = destinations->alloc_size = new_size;
22692261
}
2262+
else if (new_size > 0 && new_size <= destinations->alloc_size) {
2263+
destinations->size = new_size;
2264+
}
22702265
else {
2271-
return FBLITS_ERR_INCORRECT_ARGS_NUM;
2266+
if (new_size == 0)
2267+
return 0;
2268+
return FBLITS_ERR_INVALID_SEQUENCE_LENGTH;
22722269
}
22732270

22742271
/* load destinations */
@@ -2284,7 +2281,7 @@ _surf_fblits_cached_item_check_and_blit(pgSurfaceObject *self, PyObject *item,
22842281

22852282
if (!pg_IntFromObj(PyTuple_GET_ITEM(tup, 0), &x) ||
22862283
!pg_IntFromObj(PyTuple_GET_ITEM(tup, 1), &y)) {
2287-
return FBLITS_ERR_INCORRECT_ARGS_NUM;
2284+
return FBLITS_ERR_INVALID_DESTINATION;
22882285
}
22892286

22902287
if (x < -src->w || x > dst->w || y < -src->h || y > dst->h)
@@ -2357,6 +2354,9 @@ _surf_fblits_cached_item_check_and_blit(pgSurfaceObject *self, PyObject *item,
23572354
pgSurface_Unprep(self);
23582355
pgSurface_Unprep((pgSurfaceObject *)src_surf);
23592356

2357+
if (error == -1)
2358+
error = BLITS_ERR_BLIT_FAIL;
2359+
23602360
return error;
23612361
}
23622362

@@ -2508,6 +2508,12 @@ surf_fblits(pgSurfaceObject *self, PyObject *const *args, Py_ssize_t nargs)
25082508
"supported for this operation");
25092509
case FBLITS_ERR_NO_MEMORY:
25102510
return RAISE(PyExc_MemoryError, "No memory available");
2511+
case FBLITS_ERR_INVALID_SEQUENCE_LENGTH:
2512+
return RAISE(PyExc_ValueError,
2513+
"Invalid sequence length for cached blit");
2514+
case FBLITS_ERR_INVALID_DESTINATION:
2515+
return RAISE(PyExc_TypeError,
2516+
"Invalid destination position for cached blit");
25112517
}
25122518
return RAISE(PyExc_TypeError, "Unknown error");
25132519
}

0 commit comments

Comments
 (0)