Rename memswap() to swap_regions(). Consolidate duplicate implementations.

C99 reserves the mem* namespace for future expansion. Some Rockbox
targets had issues with memswap() conflicting with another definition,
so fix that.
This commit is contained in:
Franklin Wei
2024-07-21 18:33:01 -04:00
committed by Simon Tatham
parent 5de69c22b0
commit a2f7f962ce
4 changed files with 16 additions and 21 deletions

View File

@ -5330,6 +5330,13 @@ This macro, defined in the main Puzzles header file, strips the
modifier flags from the key code passed as an argument. It is modifier flags from the key code passed as an argument. It is
equivalent to a bitwise-AND with \cw{~MOD_MASK}. equivalent to a bitwise-AND with \cw{~MOD_MASK}.
\S{utils-swap-regions} \cw{swap_regions()}
\c void swap_regions(void *av, void *bv, size_t size);
Swap two regions of memory of \cw{size} bytes. The two regions must
not overlap.
\C{writing} How to write a new puzzle \C{writing} How to write a new puzzle
This chapter gives a guide to how to actually write a new puzzle: This chapter gives a guide to how to actually write a new puzzle:

4
misc.c
View File

@ -294,7 +294,7 @@ void game_mkhighlight(frontend *fe, float *ret,
game_mkhighlight_specific(fe, ret, background, highlight, lowlight); game_mkhighlight_specific(fe, ret, background, highlight, lowlight);
} }
static void memswap(void *av, void *bv, int size) void swap_regions(void *av, void *bv, size_t size)
{ {
char tmpbuf[512]; char tmpbuf[512];
char *a = av, *b = bv; char *a = av, *b = bv;
@ -318,7 +318,7 @@ void shuffle(void *array, int nelts, int eltsize, random_state *rs)
for (i = nelts; i-- > 1 ;) { for (i = nelts; i-- > 1 ;) {
int j = random_upto(rs, i+1); int j = random_upto(rs, i+1);
if (j != i) if (j != i)
memswap(carray + eltsize * i, carray + eltsize * j, eltsize); swap_regions(carray + eltsize * i, carray + eltsize * j, eltsize);
} }
} }

View File

@ -448,6 +448,12 @@ void copy_left_justified(char *buf, size_t sz, const char *str);
function is NULL. Dynamically allocated, to be freed by caller. */ function is NULL. Dynamically allocated, to be freed by caller. */
char *button2label(int button); char *button2label(int button);
/* Swap two regions of memory. The two regions must not
* overlap. (Note: the natural name for this might be "memswap", but
* the mem* namespace is reserved for future expansion by the C99
* standard per clause 7.26.11.1.) */
void swap_regions(void *av, void *bv, size_t size);
/* /*
* dsf.c * dsf.c
*/ */

20
sort.c
View File

@ -9,26 +9,8 @@
#include "puzzles.h" #include "puzzles.h"
static void memswap(void *av, void *bv, size_t size)
{
char t[4096];
char *a = (char *)av, *b = (char *)bv;
while (size > 0) {
size_t thissize = size < sizeof(t) ? size : sizeof(t);
memcpy(t, a, thissize);
memcpy(a, b, thissize);
memcpy(b, t, thissize);
size -= thissize;
a += thissize;
b += thissize;
}
}
#define PTR(i) ((char *)array + size * (i)) #define PTR(i) ((char *)array + size * (i))
#define SWAP(i,j) memswap(PTR(i), PTR(j), size) #define SWAP(i,j) swap_regions(PTR(i), PTR(j), size)
#define CMP(i,j) cmp(PTR(i), PTR(j), ctx) #define CMP(i,j) cmp(PTR(i), PTR(j), ctx)
#define LCHILD(i) (2*(i)+1) #define LCHILD(i) (2*(i)+1)