grub2: bring back build of aros-side grub2 tools
[AROS.git] / workbench / libs / codesets / src / utils.c
blob6d5984858a1ec670002821b22aee806d4e7495e8
1 /***************************************************************************
3 codesets.library - Amiga shared library for handling different codesets
4 Copyright (C) 2001-2005 by Alfonso [alfie] Ranieri <alforan@tin.it>.
5 Copyright (C) 2005-2014 codesets.library Open Source Team
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 codesets.library project: http://sourceforge.net/projects/codesetslib/
19 $Id$
21 ***************************************************************************/
23 #include "lib.h"
24 #include "debug.h"
26 /****************************************************************************/
28 #if !defined(HAVE_ALLOCVECPOOLED)
29 APTR allocVecPooled(APTR pool,ULONG size)
31 ULONG *mem;
33 ENTER();
35 size += sizeof(ULONG);
36 if((mem = AllocPooled(pool, size)))
37 *mem++ = size;
39 RETURN(mem);
40 return mem;
42 #endif
44 /****************************************************************************/
46 #if !defined(HAVE_FREEVECPOOLED)
47 void freeVecPooled(APTR pool,APTR mem)
49 ENTER();
51 FreePooled(pool,(LONG *)mem - 1,*((LONG *)mem - 1));
53 LEAVE();
55 #endif
57 /****************************************************************************/
59 APTR reallocVecPooled(APTR pool, APTR mem, ULONG oldSize, ULONG newSize)
61 ULONG *newMem;
63 ENTER();
65 if((newMem = allocVecPooled(pool, newSize)) != NULL)
67 memcpy(newMem, mem, (oldSize < newSize) ? oldSize : newSize);
69 freeVecPooled(pool, mem);
72 RETURN(newMem);
73 return newMem;
76 /****************************************************************************/
78 APTR allocArbitrateVecPooled(ULONG size)
80 ULONG *mem;
82 ENTER();
84 ObtainSemaphore(&CodesetsBase->poolSem);
85 mem = allocVecPooled(CodesetsBase->pool, size);
86 ReleaseSemaphore(&CodesetsBase->poolSem);
88 RETURN(mem);
89 return mem;
92 /****************************************************************************/
94 void freeArbitrateVecPooled(APTR mem)
96 ENTER();
98 ObtainSemaphore(&CodesetsBase->poolSem);
99 freeVecPooled(CodesetsBase->pool, mem);
100 ReleaseSemaphore(&CodesetsBase->poolSem);
102 LEAVE();
105 /****************************************************************************/
107 APTR reallocArbitrateVecPooled(APTR mem, ULONG oldSize, ULONG newSize)
109 ENTER();
111 ObtainSemaphore(&CodesetsBase->poolSem);
112 mem = reallocVecPooled(CodesetsBase->pool, mem, oldSize, newSize);
113 ReleaseSemaphore(&CodesetsBase->poolSem);
115 RETURN(mem);
116 return mem;
119 /****************************************************************************/
121 ULONG utf16_strlen(UTF16 *ptr)
123 ULONG l;
125 for (l=0; ptr[l]; l++);
126 return l<<1;
129 /****************************************************************************/
131 ULONG utf32_strlen(UTF32 *ptr)
133 ULONG l;
135 for (l=0; ptr[l]; l++);
136 return l<<2;
139 /****************************************************************************/
141 // GetHead()
142 // get the head element of a list
143 #if !defined(HAVE_GETHEAD)
144 struct Node *GetHead(struct List *list)
146 struct Node *result = NULL;
148 if(list != NULL && IsListEmpty(list) == FALSE)
149 result = list->lh_Head;
151 return result;
153 #endif
155 /****************************************************************************/
157 // GetPred()
158 // get a node's predecessor
159 #if !defined(HAVE_GETPRED)
160 struct Node *GetPred(struct Node *node)
162 struct Node *result = NULL;
164 if(node != NULL && node->ln_Pred != NULL && node->ln_Pred->ln_Pred != NULL)
165 result = node->ln_Pred;
167 return result;
169 #endif
171 /****************************************************************************/
173 // GetSucc()
174 // get a node's sucessor
175 #if !defined(HAVE_GETSUCC)
176 struct Node *GetSucc(struct Node *node)
178 struct Node *result = NULL;
180 if(node != NULL && node->ln_Succ != NULL && node->ln_Succ->ln_Succ != NULL)
181 result = node->ln_Succ;
183 return result;
185 #endif
187 /****************************************************************************/
189 // GetTail()
190 // get the tail element of a list
191 #if !defined(HAVE_GETTAIL)
192 struct Node *GetTail(struct List *list)
194 struct Node *result = NULL;
196 if(list != NULL && IsListEmpty(list) == FALSE)
197 result = list->lh_TailPred;
199 return result;
201 #endif