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