Skip to content

Commit

Permalink
Add type definitions and stub module for in-memory tape. Add linked l…
Browse files Browse the repository at this point in the history
…ist module (HelenOS-compatible).
  • Loading branch information
jxsvoboda committed Aug 23, 2018
1 parent aca60bf commit 5849f22
Show file tree
Hide file tree
Showing 9 changed files with 965 additions and 6 deletions.
2 changes: 2 additions & 0 deletions Makefile
Expand Up @@ -25,6 +25,8 @@ LIBS_helenos = `helenos-pkg-config --libs libgui libdraw libmath libhound libpc
bkqual = $$(date '+%Y-%m-%d')

sources_generic = \
adt/list.c \
tape/tape.c \
wav/chunk.c \
wav/rwave.c \
fileutil.c \
Expand Down
167 changes: 167 additions & 0 deletions adt/list.c
@@ -0,0 +1,167 @@
/*
* GZX - George's ZX Spectrum Emulator
* Linked list
*
* Copyright (c) 1999-2018 Jiri Svoboda
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#include "../adt/list.h"

/** Initialize list */
void list_initialize(list_t *list)
{
list->head.prev = &list->head;
list->head.next = &list->head;
}

/** Initialize link */
void link_initialize(link_t *link)
{
link->prev = NULL;
link->next = NULL;
}

/** Insert item before item in list */
void list_insert_before(link_t *nlink, link_t *olink)
{
assert(!link_used(nlink));

olink->prev->next = nlink;
nlink->prev = olink->prev;
nlink->next = olink;
olink->prev = nlink;
}

/** Insert item after item in list */
void list_insert_after(link_t *nlink, link_t *olink)
{
assert(!link_used(nlink));

olink->next->prev = nlink;
nlink->next = olink->next;
nlink->prev = olink;
olink->next = nlink;
}

/** Insert at beginning of list */
void list_prepend(link_t *link, list_t *list)
{
list_insert_after(link, &list->head);
}

/** Insert at end of list */
void list_append(link_t *link, list_t *list)
{
list_insert_before(link, &list->head);
}

/** Remove item from list */
void list_remove(link_t *link)
{
assert(link_used(link));

link->prev->next = link->next;
link->next->prev = link->prev;

link->prev = NULL;
link->next = NULL;
}

/** Return true if item is linked to a list */
bool link_used(link_t *link)
{
if (link->prev == NULL && link->next == NULL)
return false;

assert(link->prev != NULL && link->next != NULL);
return true;
}

/** Return true if list is empty */
bool list_empty(list_t *list)
{
return list->head.next == &list->head;
}

/** Return the number of entries in @a list. */
unsigned long list_count(list_t *list)
{
link_t *link;
unsigned long count;

count = 0;
link = list_first(list);
while (link != NULL) {
++count;
link = list_next(link, list);
}

return count;
}

/** Return first item in a list or @c NULL if list is empty */
link_t *list_first(list_t *list)
{
if (list->head.next == &list->head)
return NULL;

return list->head.next;
}

/** Return last item in a list or @c NULL if list is empty */
link_t *list_last(list_t *list)
{
if (list->head.prev == &list->head)
return NULL;

return list->head.prev;
}

/** Return previous item in list or @c NULL if @a link is the first one */
link_t *list_prev(link_t *link, list_t *list)
{
assert(link_used(link));

if (link->prev == &list->head)
return NULL;

return link->prev;
}

/** Return next item in list or @c NULL if @a link is the last one */
link_t *list_next(link_t *link, list_t *list)
{
assert(link_used(link));

if (link->next == &list->head)
return NULL;

return link->next;
}
69 changes: 69 additions & 0 deletions adt/list.h
@@ -0,0 +1,69 @@
/*
* GZX - George's ZX Spectrum Emulator
* Linked list
*
* Copyright (c) 1999-2018 Jiri Svoboda
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef ADT_LIST_H
#define ADT_LIST_H

#include <stdbool.h>
#include <stddef.h>
#include "../types/adt/list.h"

#define list_get_instance(link, type, member) \
((type *)( (void *)(link) - ((void *) &((type *) NULL)->member)))

#define list_foreach(list, member, itype, iterator) \
for (itype *iterator = NULL; iterator == NULL; iterator = (itype *) 1) \
for (link_t *_link = (list).head.next; \
iterator = list_get_instance(_link, itype, member), \
_link != &(list).head; _link = _link->next)

#define list_foreach_rev(list, member, itype, iterator) \
for (itype *iterator = NULL; iterator == NULL; iterator = (itype *) 1) \
for (link_t *_link = (list).head.prev; \
iterator = list_get_instance(_link, itype, member), \
_link != &(list).head; _link = _link->prev)

extern void list_initialize(list_t *);
extern void link_initialize(link_t *);
extern void list_insert_before(link_t *, link_t *);
extern void list_insert_after(link_t *, link_t *);
extern void list_prepend(link_t *, list_t *);
extern void list_append(link_t *, list_t *);
extern void list_remove(link_t *);
extern bool link_used(link_t *);
extern bool list_empty(list_t *);
extern unsigned long list_count(list_t *);
extern link_t *list_first(list_t *);
extern link_t *list_last(list_t *);
extern link_t *list_prev(link_t *, list_t *);
extern link_t *list_next(link_t *, list_t *);

#endif
65 changes: 65 additions & 0 deletions tape/tape.c
@@ -0,0 +1,65 @@
/*
* GZX - George's ZX Spectrum Emulator
* Spectrum tape
*
* Copyright (c) 1999-2018 Jiri Svoboda
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <assert.h>
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#include "../adt/list.h"
#include "../types/tape/tape.h"
#include "tape.h"

/** Create empty tape.
*
* @param rtape Place to store pointer to new tape.
* @return Zero on success or error code
*/
int tape_create(tape_t **rtape)
{
tape_t *tape;

tape = calloc(1, sizeof(tape_t));
if (tape == NULL)
return ENOMEM;

list_initialize(&tape->blocks);

return 0;
}

/** Destroy tape.
*
* @param tape Tape
*/
void tape_destroy(tape_t *tape)
{
free(tape);
}
40 changes: 40 additions & 0 deletions tape/tape.h
@@ -0,0 +1,40 @@
/*
* GZX - George's ZX Spectrum Emulator
* Spectrum tape
*
* Copyright (c) 1999-2018 Jiri Svoboda
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef TAPE_TAPE_H
#define TAPE_TAPE_H

#include <stdint.h>
#include "../types/tape/tape.h"

int tape_create(tape_t **);
void tape_destroy(tape_t *);

#endif
45 changes: 45 additions & 0 deletions types/adt/list.h
@@ -0,0 +1,45 @@
/*
* GZX - George's ZX Spectrum Emulator
* Linked list types
*
* Copyright (c) 1999-2018 Jiri Svoboda
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef TYPES_ADT_LIST_H
#define TYPES_ADT_LIST_H

/** List link */
typedef struct link {
struct link *prev, *next;
} link_t;

/** Doubly linked list */
typedef struct {
link_t head;
} list_t;

#endif

0 comments on commit 5849f22

Please sign in to comment.