Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add two new queue macros with tests, plus a warning fix. #94

Merged
merged 3 commits into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,15 @@ Wrapped headers and replaced functions are:
<td>OSX10.10</td>
</tr>
<tr>
<td><code>sys/queue.h</code></td>
<td rowspan="3"><code>sys/queue.h</code></td>
<td>Adds <code>SLIST_HEAD_INITIALIZER</code> macro</td>
<td>OSX10.4</td>
</tr>
<tr>
<td>Adds <code>SLIST_REMOVE_AFTER</code> macro</td>
<td>OSX10.6</td>
</tr>
<tr>
<td>Adds <code>STAILQ_FOREACH</code> macro</td>
<td>OSX10.4</td>
</tr>
Expand Down
10 changes: 8 additions & 2 deletions include/MacportsLegacySupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,14 @@
/* PTHREAD_RWLOCK_INITIALIZER is not defined until 10.5 */
/* The addition uses an #ifndef, so no feature flag is necessary */

/* STAILQ_FOREACH is not defined until 10.5 */
/* The addition uses an #ifndef, so no feature flag is necessary */
/* Macros missing from some earlier versions of sys/queue.h */
/* All additions use #ifndef, so no feature flags are necessary. */

/* Missing until 10.5 */
/* SLIST_HEAD_INITIALIZER, STAILQ_FOREACH */

/* Missing until 10.7 */
/* SLIST_REMOVE_AFTER */

/* c++11 <cmath> PPC 10.[45] and Intel 10.[4-6], GNU g++ 4.6 through 8. */
#if (__MPLS_TARGET_OSVER < 1070 \
Expand Down
23 changes: 22 additions & 1 deletion include/sys/queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,28 @@
/* Include the primary system sys/queue.h */
#include_next <sys/queue.h>

/* STAILQ_FOREACH is not defined on Tiger */
/* SLIST functions missing from earlier SDK versions */

/* Missing until 10.5 */

#ifndef SLIST_HEAD_INITIALIZER
#define SLIST_HEAD_INITIALIZER(head) \
{ NULL }
#endif

/* Missing until 10.7 */

#ifndef SLIST_REMOVE_AFTER
#define SLIST_REMOVE_AFTER(elm, field) do { \
SLIST_NEXT(elm, field) = \
SLIST_NEXT(SLIST_NEXT(elm, field), field); \
} while (0)
#endif

/* STAILQ functions missing from earlier SDK versions */

/* Missing until 10.5 */

#ifndef STAILQ_FOREACH
#define STAILQ_FOREACH(var, head, field) \
for((var) = STAILQ_FIRST((head)); \
Expand Down
3 changes: 1 addition & 2 deletions test/test_fstatat64.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@
#include <errno.h>
#include <fcntl.h>

#define FEEDBACK 1

/*
* For newer macOS releases - where this syscall is available - it's declared
* in header 'sys/sysproto.h', within 'Kernel.framework'.
*
* To avoid depending on that, declare it ourselves. (Remember that C/C++
* allows redundant function declarations, as long as the signatures match.)
*/
struct stat64; /* Avoid warning on 10.4 (where this doesn't work, anyway) */
int fstatat64(int dirfd, const char *pathname, struct stat64 *buf, int flags);

int main (int argc, char **argv) {
Expand Down
131 changes: 131 additions & 0 deletions test/test_queues.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* Copyright (c) 2024 Frederick H. G. Wright II <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

/*
* This provides tests for some functions from sys/queue.h., currently:
* STAILQ_FOREACH
* SLIST_REMOVE_AFTER
*/

#include <assert.h>
#include <stddef.h> /* For NULL */
#include <sys/queue.h>


/* Tests for SLIST_REMOVE_AFTER */

#ifndef SLIST_REMOVE_AFTER
#error SLIST_REMOVE_AFTER is undefined
#endif

#ifndef SLIST_HEAD_INITIALIZER
#error SLIST_HEAD_INITIALIZER is undefined
#endif

typedef struct slist_entry_s {
int value;
SLIST_ENTRY(slist_entry_s) next;
} slist_entry_t;

typedef SLIST_HEAD(slist_head_s, slist_entry_s) slist_head_t;

static slist_head_t slist_head = SLIST_HEAD_INITIALIZER(slist_head);

/* Some sample entries */
static slist_entry_t slist_entries[] = {
{.value = 1},
{.value = 2},
{.value = 3},
};
#define SLIST_NUM (sizeof(slist_entries) / sizeof(slist_entries[0]))

static void
test_slist(void)
{
slist_entry_t *ep, *tp;

/* Fill the queue with the sample entries (reversed) */
for (ep = &slist_entries[0]; ep < &slist_entries[SLIST_NUM]; ++ep) {
SLIST_INSERT_HEAD(&slist_head, ep, next);
}

/* Get and check the first entry */
ep = &slist_entries[SLIST_NUM - 1];
tp = SLIST_FIRST(&slist_head);
assert(tp->value == ep->value);

/* Remove the following entry, then check the one after */
SLIST_REMOVE_AFTER(tp, next);
ep = &slist_entries[SLIST_NUM - 1 - 2];
tp = SLIST_NEXT(tp, next);
assert(tp->value == ep->value);
}


/* Tests for STAILQ_FOREACH */

#ifndef STAILQ_FOREACH
#error STAILQ_FOREACH is undefined
#endif

typedef struct stailq_entry_s {
int value;
STAILQ_ENTRY(stailq_entry_s) next;
} stailq_entry_t;

typedef STAILQ_HEAD(stailq_head_s, stailq_entry_s) stailq_head_t;

static stailq_head_t stailq_head = STAILQ_HEAD_INITIALIZER(stailq_head);

/* Some sample entries */
static stailq_entry_t stailq_entries[] = {
{.value = 1},
{.value = 2},
{.value = 3},
};
#define STAILQ_NUM (sizeof(stailq_entries) / sizeof(stailq_entries[0]))

static void
test_stailq(void)
{
stailq_entry_t *ep, *tp;

/* Fill the queue with the sample entries */
for (ep = &stailq_entries[0]; ep < &stailq_entries[STAILQ_NUM]; ++ep) {
STAILQ_INSERT_TAIL(&stailq_head, ep, next);
}

/* See if STAILQ_FOREACH returns expected sequence */
ep = &stailq_entries[0];
STAILQ_FOREACH(tp, &stailq_head, next) {
assert(tp->value == ep++->value);
}

/* Check expected end */
assert(ep == &stailq_entries[STAILQ_NUM]);
}


int
main(int argc, char *argv[])
{
(void) argc; (void) argv;

test_slist();
test_stailq();

return 0;
}