correct feedback channels
[privoxy.git] / amiga.c
1 const char amiga_rcs[] = "$Id: amiga.c,v 1.5 2002/03/03 09:18:03 joergs Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/amiga.c,v $
5  *
6  * Purpose     :  Amiga-specific declarations.
7  *
8  * Copyright   :  Written by and Copyright (C) 2001 the SourceForge
9  *                IJBSWA team.  http://ijbswa.sourceforge.net
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 1.5  2002/03/03 09:18:03  joergs
32  *    Made jumbjuster work on AmigaOS again.
33  *
34  *    Revision 1.4  2001/10/07 15:35:13  oes
35  *    Replaced 6 boolean members of csp with one bitmap (csp->flags)
36  *
37  *    Revision 1.3  2001/09/12 22:54:51  joergs
38  *    Stacksize of main thread increased.
39  *
40  *    Revision 1.2  2001/05/23 00:13:58  joergs
41  *    AmigaOS support fixed.
42  *
43  *    Revision 1.1.1.1  2001/05/15 13:58:46  oes
44  *    Initial import of version 2.9.3 source tree
45  *
46  *
47  *********************************************************************/
48 \f
49
50 #include "config.h"
51
52 #ifdef AMIGA
53
54 #include <stdio.h>
55 #include <signal.h>
56
57 #include "project.h"
58
59 const char amiga_h_rcs[] = AMIGA_H_VERSION;
60
61 unsigned long __stack = 100*1024;
62 static char ver[] = "$VER: junkbuster " __AMIGAVERSION__ " (" __AMIGADATE__ ")";
63 struct Task *main_task = NULL;
64 int childs = 0;
65
66 void serve(struct client_state *csp);
67
68 __saveds ULONG server_thread(void)
69 {
70    struct client_state *local_csp;
71    struct UserData UserData;
72    struct Task *me=FindTask(NULL);
73
74    Wait(SIGF_SINGLE);
75    local_csp=(struct client_state *)(me->tc_UserData);
76    me->tc_UserData=&UserData;
77    SocketBase=(APTR)OpenLibrary("bsdsocket.library",3);
78    if(SocketBase)
79    {
80       SetErrnoPtr(&(UserData.eno),sizeof(int));
81       local_csp->cfd=ObtainSocket(local_csp->cfd, AF_INET, SOCK_STREAM, 0);
82       if(JB_INVALID_SOCKET!=local_csp->cfd)
83       {
84          Signal(main_task,SIGF_SINGLE);
85          serve((struct client_state *) local_csp);
86       } else {
87          local_csp->flags &= ~CSP_FLAG_ACTIVE;
88          Signal(main_task,SIGF_SINGLE);
89       }
90       CloseLibrary(SocketBase);
91    } else {
92       local_csp->flags &= ~CSP_FLAG_ACTIVE;
93       Signal(main_task,SIGF_SINGLE);
94    }
95    childs--;
96    return 0;
97 }
98
99 static BPTR olddir;
100
101 void amiga_exit(void)
102 {
103    if(SocketBase)
104    {
105       CloseLibrary(SocketBase);
106    }
107    CurrentDir(olddir);
108 }
109
110 static struct SignalSemaphore memsem;
111 static struct SignalSemaphore *memsemptr = NULL;
112 static struct UserData GlobalUserData;
113
114 void InitAmiga(void)
115 {
116    main_task = FindTask(NULL);
117    main_task->tc_UserData = &GlobalUserData;
118
119    if (((struct Library *)SysBase)->lib_Version < 39)
120    {
121       exit(RETURN_FAIL);
122    }
123
124    signal(SIGINT,SIG_IGN);
125    SocketBase = (APTR)OpenLibrary("bsdsocket.library",3);
126    if (!SocketBase)
127    {
128       fprintf(stderr, "Can't open bsdsocket.library V3+\n");
129       exit(RETURN_ERROR);
130    }
131    SetErrnoPtr(&(GlobalUserData.eno),sizeof(int));
132    InitSemaphore(&memsem);
133    memsemptr = &memsem;
134
135    olddir=CurrentDir(GetProgramDir());
136    atexit(amiga_exit);
137 }
138
139 #ifdef __GNUC__
140 #ifdef libnix
141 /* multithreadingsafe libnix replacements */
142 static void *memPool=NULL;
143
144 void *malloc (size_t s)
145 {
146    ULONG *mem;
147    LONG size = s;
148
149    if (size<=0)
150    {
151       return NULL;
152    }
153    if (!memPool)
154    {
155       if (!(memPool=CreatePool(MEMF_ANY,32*1024,8*1024)))
156       {
157          return NULL;
158       }
159    }
160    size += sizeof(ULONG) + MEM_BLOCKMASK;
161    size &= ~MEM_BLOCKMASK;
162    if (memsemptr)
163    {
164       ObtainSemaphore(memsemptr);
165    }
166    if ((mem=AllocPooled(memPool,size)))
167    {
168       *mem++=size;
169    }
170    if (memsemptr)
171    {
172       ReleaseSemaphore(memsemptr);
173    }
174    return mem;
175 }
176
177 void free (void *m)
178 {
179    ULONG *mem = m;
180
181    if(mem && memPool)
182    {
183       ULONG size=*--mem;
184
185       if (memsemptr)
186       {
187          ObtainSemaphore(memsemptr);
188       }
189       FreePooled(memPool,mem,size);
190       if (memsemptr)
191       {
192          ReleaseSemaphore(memsemptr);
193       }
194    }
195 }
196
197 void *realloc (void *old, size_t ns)
198 {
199    void *new;
200    LONG osize, *o = old;
201    LONG nsize = ns;
202
203    if (!old)
204    {
205       return malloc(nsize);
206    }
207    osize = (*(o-1)) - sizeof(ULONG);
208    if (nsize <= osize)
209    {
210       return old;
211    }
212    if ((new = malloc(nsize)))
213    {
214       ULONG *n = new;
215
216       osize >>= 2;
217       while(osize--)
218       {
219          *n++ = *o++;
220       }
221       free(old);
222    }
223    return new;
224 }
225
226 void __memCleanUp (void)
227 {
228    if (memsemptr)
229    {
230       ObtainSemaphore(memsemptr);
231    }
232    if (memPool)
233    {
234       DeletePool(memPool);
235    }
236    if (memsemptr)
237    {
238       ReleaseSemaphore(memsemptr);
239    }
240 }
241
242 #define ADD2LIST(a,b,c) asm(".stabs \"_" #b "\"," #c ",0,0,_" #a )
243 #define ADD2EXIT(a,pri) ADD2LIST(a,__EXIT_LIST__,22); \
244                         asm(".stabs \"___EXIT_LIST__\",20,0,0," #pri "+128")
245 ADD2EXIT(__memCleanUp,-50);
246 #elif !defined(ixemul)
247 #error No libnix and no ixemul!?
248 #endif /* libnix */
249 #else
250 #error Only GCC is supported, multithreading safe malloc/free required.
251 #endif /* __GNUC__ */
252
253 #endif /* def AMIGA */