NetSurf
object.c
Go to the documentation of this file.
1/*
2 * Copyright 2005, 2008, 2016 Chris Young <chris@unsatisfactorysoftware.co.uk>
3 *
4 * This file is part of NetSurf, http://www.netsurf-browser.org/
5 *
6 * NetSurf is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * NetSurf is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include "amiga/os3support.h"
20
21#include <stdbool.h>
22#include <stdlib.h>
23#include <string.h>
24
25#include <proto/exec.h>
26#include <exec/lists.h>
27#include <exec/nodes.h>
28
29#include "amiga/memory.h"
30#include "amiga/object.h"
31
32#ifdef __amigaos4__
33#define nsList MinList
34#define NewnsList NewMinList
35#else
36#define nsList List
37#define NewnsList NewList
38#endif
39
40static APTR pool_nsobj = NULL;
41
43{
45
46 if(pool_nsobj == NULL) return false;
47 else return true;
48}
49
51{
53}
54
55/* Slightly abstract MinList initialisation */
56static void ami_NewMinList(struct MinList *list)
57{
58 if(list == NULL) return;
59 NewnsList((struct nsList *)list);
60}
61
62/* Allocate and initialise a new MinList */
63struct MinList *ami_AllocMinList(void)
64{
65 struct MinList *objlist = (struct MinList *)malloc(sizeof(struct nsList));
66 if(objlist == NULL) return NULL;
67 ami_NewMinList(objlist);
68 return objlist;
69}
70
71struct MinList *NewObjList(void)
72{
73 struct MinList *objlist = ami_AllocMinList();
74 return(objlist);
75}
76
77struct nsObject *AddObject(struct MinList *objlist, ULONG otype)
78{
79 struct nsObject *dtzo;
80
81 dtzo = (struct nsObject *)ami_memory_itempool_alloc(pool_nsobj, sizeof(struct nsObject));
82 if(dtzo == NULL) return NULL;
83
84 memset(dtzo, 0, sizeof(struct nsObject));
85 AddTail((struct List *)objlist,(struct Node *)dtzo);
86
87 dtzo->Type = otype;
88
89 return(dtzo);
90}
91
92void ObjectCallback(struct nsObject *dtzo, void (*callback)(void *nso))
93{
94 dtzo->callback = callback;
95}
96
97static void DelObjectInternal(struct nsObject *dtzo, BOOL free_obj)
98{
99 Remove((struct Node *)dtzo);
100 if(dtzo->callback != NULL) dtzo->callback(dtzo->objstruct);
101 if(dtzo->objstruct && free_obj) free(dtzo->objstruct);
102 if(dtzo->dtz_Node.ln_Name) free(dtzo->dtz_Node.ln_Name);
103 ami_memory_itempool_free(pool_nsobj, dtzo, sizeof(struct nsObject));
104 dtzo = NULL;
105}
106
107void DelObject(struct nsObject *dtzo)
108{
109 DelObjectInternal(dtzo, TRUE);
110}
111
112void DelObjectNoFree(struct nsObject *dtzo)
113{
114 DelObjectInternal(dtzo, FALSE);
115}
116
117void FreeObjList(struct MinList *objlist)
118{
119 struct nsObject *node;
120 struct nsObject *nnode;
121
122 if(IsMinListEmpty((struct MinList *)objlist) == FALSE) {
123 node = (struct nsObject *)GetHead((struct List *)objlist);
124
125 do {
126 nnode = (struct nsObject *)GetSucc((struct Node *)node);
127 if(node->Type == AMINS_RECT) {
128 DelObjectNoFree(node);
129 } else {
130 DelObject(node);
131 }
132 } while((node = nnode));
133 }
134 free(objlist);
135}
136
struct MinList * ami_AllocMinList(void)
List abstraction as OS3 appears to have problems with NewMinList()
Definition: object.c:63
struct MinList * NewObjList(void)
Definition: object.c:71
static void DelObjectInternal(struct nsObject *dtzo, BOOL free_obj)
Definition: object.c:97
void FreeObjList(struct MinList *objlist)
Definition: object.c:117
void ami_object_fini(void)
Definition: object.c:50
bool ami_object_init(void)
Initialisation for itempool.
Definition: object.c:42
void ObjectCallback(struct nsObject *dtzo, void(*callback)(void *nso))
Definition: object.c:92
void DelObject(struct nsObject *dtzo)
Definition: object.c:107
static void ami_NewMinList(struct MinList *list)
Definition: object.c:56
#define NewnsList
Definition: object.c:37
struct nsObject * AddObject(struct MinList *objlist, ULONG otype)
Definition: object.c:77
#define nsList
Definition: object.c:36
static APTR pool_nsobj
Definition: object.c:40
void DelObjectNoFree(struct nsObject *dtzo)
Definition: object.c:112
@ AMINS_RECT
Definition: object.h:39
#define ami_memory_itempool_create(s)
Definition: memory.h:54
#define ami_memory_itempool_alloc(p, s)
Definition: memory.h:56
#define ami_memory_itempool_free(p, i, s)
Definition: memory.h:57
#define ami_memory_itempool_delete(p)
Definition: memory.h:55
struct Node * GetHead(struct List *list)
Definition: os3support.c:364
struct Node * GetSucc(struct Node *node)
Definition: os3support.c:381
Minimal compatibility header for AmigaOS 3.
#define IsMinListEmpty(L)
Definition: os3support.h:54
Interface to utility string handling.
struct Node dtz_Node
Definition: object.h:44
void(* callback)(void *nso)
Definition: object.h:48
ULONG Type
Definition: object.h:45
void * objstruct
Definition: object.h:46