97d9f75ae99780d90b31fcba7496eca4e1d1fcc6
[privoxy.git] / src / amiga.c
1 const char amiga_rcs[] = "$Id: amiga.c,v 2.0 2002/06/04 14:34:21 jongfoster Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/src/amiga.c,v $
5  *
6  * Purpose     :  Amiga-specific declarations.
7  *
8  * Copyright   :  Written by and Copyright (C) 2001 the SourceForge
9  *                Privoxy team. http://www.privoxy.org/
10  *
11  *                This program is free software; you can redistribute it 
12  *                and/or modify it under the terms of the GNU General
13  *                Public License as published by the Free Software
14  *                Foundation; either version 2 of the License, or (at
15  *                your option) any later version.
16  *
17  *                This program is distributed in the hope that it will
18  *                be useful, but WITHOUT ANY WARRANTY; without even the
19  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
20  *                PARTICULAR PURPOSE.  See the GNU General Public
21  *                License for more details.
22  *
23  *                The GNU General Public License should be included with
24  *                this file.  If not, you can view it at
25  *                http://www.gnu.org/copyleft/gpl.html
26  *                or write to the Free Software Foundation, Inc., 59
27  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
28  *
29  * Revisions   :
30  *    $Log: amiga.c,v $
31  *    Revision 2.0  2002/06/04 14:34:21  jongfoster
32  *    Moving source files to src/
33  *
34  *    Revision 1.9  2002/03/26 22:29:54  swa
35  *    we have a new homepage!
36  *
37  *    Revision 1.8  2002/03/25 19:32:15  joergs
38  *    Name in version string changed from junkbuster to Privoxy.
39  *
40  *    Revision 1.7  2002/03/24 13:25:43  swa
41  *    name change related issues
42  *
43  *    Revision 1.6  2002/03/09 20:03:52  jongfoster
44  *    - Making various functions return int rather than size_t.
45  *      (Undoing a recent change).  Since size_t is unsigned on
46  *      Windows, functions like read_socket that return -1 on
47  *      error cannot return a size_t.
48  *
49  *      THIS WAS A MAJOR BUG - it caused frequent, unpredictable
50  *      crashes, and also frequently caused JB to jump to 100%
51  *      CPU and stay there.  (Because it thought it had just
52  *      read ((unsigned)-1) == 4Gb of data...)
53  *
54  *    - The signature of write_socket has changed, it now simply
55  *      returns success=0/failure=nonzero.
56  *
57  *    - Trying to get rid of a few warnings --with-debug on
58  *      Windows, I've introduced a new type "jb_socket".  This is
59  *      used for the socket file descriptors.  On Windows, this
60  *      is SOCKET (a typedef for unsigned).  Everywhere else, it's
61  *      an int.  The error value can't be -1 any more, so it's
62  *      now JB_INVALID_SOCKET (which is -1 on UNIX, and in
63  *      Windows it maps to the #define INVALID_SOCKET.)
64  *
65  *    - The signature of bind_port has changed.
66  *
67  *    Revision 1.5  2002/03/03 09:18:03  joergs
68  *    Made jumbjuster work on AmigaOS again.
69  *
70  *    Revision 1.4  2001/10/07 15:35:13  oes
71  *    Replaced 6 boolean members of csp with one bitmap (csp->flags)
72  *
73  *    Revision 1.3  2001/09/12 22:54:51  joergs
74  *    Stacksize of main thread increased.
75  *
76  *    Revision 1.2  2001/05/23 00:13:58  joergs
77  *    AmigaOS support fixed.
78  *
79  *    Revision 1.1.1.1  2001/05/15 13:58:46  oes
80  *    Initial import of version 2.9.3 source tree
81  *
82  *
83  *********************************************************************/
84 \f
85
86 #include "config.h"
87
88 #ifdef AMIGA
89
90 #include <stdio.h>
91 #include <signal.h>
92
93 #include "project.h"
94
95 const char amiga_h_rcs[] = AMIGA_H_VERSION;
96
97 unsigned long __stack = 100*1024;
98 static char ver[] = "$VER: Privoxy " __AMIGAVERSION__ " (" __AMIGADATE__ ")";
99 struct Task *main_task = NULL;
100 int childs = 0;
101
102 void serve(struct client_state *csp);
103
104 __saveds ULONG server_thread(void)
105 {
106    struct client_state *local_csp;
107    struct UserData UserData;
108    struct Task *me=FindTask(NULL);
109
110    Wait(SIGF_SINGLE);
111    local_csp=(struct client_state *)(me->tc_UserData);
112    me->tc_UserData=&UserData;
113    SocketBase=(APTR)OpenLibrary("bsdsocket.library",3);
114    if(SocketBase)
115    {
116       SetErrnoPtr(&(UserData.eno),sizeof(int));
117       local_csp->cfd=ObtainSocket(local_csp->cfd, AF_INET, SOCK_STREAM, 0);
118       if(JB_INVALID_SOCKET!=local_csp->cfd)
119       {
120          Signal(main_task,SIGF_SINGLE);
121          serve((struct client_state *) local_csp);
122       } else {
123          local_csp->flags &= ~CSP_FLAG_ACTIVE;
124          Signal(main_task,SIGF_SINGLE);
125       }
126       CloseLibrary(SocketBase);
127    } else {
128       local_csp->flags &= ~CSP_FLAG_ACTIVE;
129       Signal(main_task,SIGF_SINGLE);
130    }
131    childs--;
132    return 0;
133 }
134
135 static BPTR olddir;
136
137 void amiga_exit(void)
138 {
139    if(SocketBase)
140    {
141       CloseLibrary(SocketBase);
142    }
143    CurrentDir(olddir);
144 }
145
146 static struct SignalSemaphore memsem;
147 static struct SignalSemaphore *memsemptr = NULL;
148 static struct UserData GlobalUserData;
149
150 void InitAmiga(void)
151 {
152    main_task = FindTask(NULL);
153    main_task->tc_UserData = &GlobalUserData;
154
155    if (((struct Library *)SysBase)->lib_Version < 39)
156    {
157       exit(RETURN_FAIL);
158    }
159
160    signal(SIGINT,SIG_IGN);
161    SocketBase = (APTR)OpenLibrary("bsdsocket.library",3);
162    if (!SocketBase)
163    {
164       fprintf(stderr, "Can't open bsdsocket.library V3+\n");
165       exit(RETURN_ERROR);
166    }
167    SetErrnoPtr(&(GlobalUserData.eno),sizeof(int));
168    InitSemaphore(&memsem);
169    memsemptr = &memsem;
170
171    olddir=CurrentDir(GetProgramDir());
172    atexit(amiga_exit);
173 }
174
175 #ifdef __GNUC__
176 #ifdef libnix
177 /* multithreadingsafe libnix replacements */
178 static void *memPool=NULL;
179
180 void *malloc (size_t s)
181 {
182    ULONG *mem;
183    LONG size = s;
184
185    if (size<=0)
186    {
187       return NULL;
188    }
189    if (!memPool)
190    {
191       if (!(memPool=CreatePool(MEMF_ANY,32*1024,8*1024)))
192       {
193          return NULL;
194       }
195    }
196    size += sizeof(ULONG) + MEM_BLOCKMASK;
197    size &= ~MEM_BLOCKMASK;
198    if (memsemptr)
199    {
200       ObtainSemaphore(memsemptr);
201    }
202    if ((mem=AllocPooled(memPool,size)))
203    {
204       *mem++=size;
205    }
206    if (memsemptr)
207    {
208       ReleaseSemaphore(memsemptr);
209    }
210    return mem;
211 }
212
213 void free (void *m)
214 {
215    ULONG *mem = m;
216
217    if(mem && memPool)
218    {
219       ULONG size=*--mem;
220
221       if (memsemptr)
222       {
223          ObtainSemaphore(memsemptr);
224       }
225       FreePooled(memPool,mem,size);
226       if (memsemptr)
227       {
228          ReleaseSemaphore(memsemptr);
229       }
230    }
231 }
232
233 void *realloc (void *old, size_t ns)
234 {
235    void *new_buf;
236    LONG osize, *o = old;
237    LONG nsize = ns;
238
239    if (!old)
240    {
241       return malloc(nsize);
242    }
243    osize = (*(o-1)) - sizeof(ULONG);
244    if (nsize <= osize)
245    {
246       return old;
247    }
248    if ((new_buf = malloc(nsize)))
249    {
250       ULONG *n = new_buf;
251
252       osize >>= 2;
253       while(osize--)
254       {
255          *n++ = *o++;
256       }
257       free(old);
258    }
259    return new_buf;
260 }
261
262 void __memCleanUp (void)
263 {
264    if (memsemptr)
265    {
266       ObtainSemaphore(memsemptr);
267    }
268    if (memPool)
269    {
270       DeletePool(memPool);
271    }
272    if (memsemptr)
273    {
274       ReleaseSemaphore(memsemptr);
275    }
276 }
277
278 #define ADD2LIST(a,b,c) asm(".stabs \"_" #b "\"," #c ",0,0,_" #a )
279 #define ADD2EXIT(a,pri) ADD2LIST(a,__EXIT_LIST__,22); \
280                         asm(".stabs \"___EXIT_LIST__\",20,0,0," #pri "+128")
281 ADD2EXIT(__memCleanUp,-50);
282 #elif !defined(ixemul)
283 #error No libnix and no ixemul!?
284 #endif /* libnix */
285 #else
286 #error Only GCC is supported, multithreading safe malloc/free required.
287 #endif /* __GNUC__ */
288
289 #endif /* def AMIGA */