]> git.rkrishnan.org Git - dttsp.git/blob - jDttSP/win/chan.c
Initial revision
[dttsp.git] / jDttSP / win / chan.c
1 /* chan.c\r
2 \r
3 fast inter-user-process single-reader/single-writer channels\r
4 using a modified version of jack ringbuffers and memory-mapped files.\r
5    \r
6 This file is part of a program that implements a Software-Defined Radio.\r
7 \r
8 Copyright (C) 2004 by Frank Brickle, AB2KT and Bob McGwier, N4HY\r
9 \r
10 This program is free software; you can redistribute it and/or modify\r
11 it under the terms of the GNU General Public License as published by\r
12 the Free Software Foundation; either version 2 of the License, or\r
13 (at your option) any later version.\r
14 \r
15 This program is distributed in the hope that it will be useful,\r
16 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
18 GNU General Public License for more details.\r
19 \r
20 You should have received a copy of the GNU General Public License\r
21 along with this program; if not, write to the Free Software\r
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
23 \r
24 The authors can be reached by email at\r
25 \r
26 ab2kt@arrl.net\r
27 or\r
28 rwmcgwier@comcast.net\r
29 \r
30 or by paper mail at\r
31 \r
32 The DTTS Microwave Society\r
33 6 Kathleen Place\r
34 Bridgewater, NJ 08807 \r
35 */\r
36 \r
37 #include <chan.h>\r
38 \r
39 //------------------------------------------------------------------------\r
40 \r
41 size_t\r
42 putChan(Chan c, char *data, size_t size) {\r
43   return ringb_write(c->rb, data, size);\r
44 }\r
45 \r
46 size_t\r
47 getChan(Chan c, char *data, size_t size) {\r
48   return ringb_read(c->rb, data, size);\r
49 }\r
50 \r
51 BOOLEAN\r
52 putChan_nowait(Chan c, char *data, size_t size) {\r
53   if (ringb_write_space(c->rb) >= size) {\r
54     ringb_write(c->rb, data, size);\r
55     return TRUE;\r
56   } else return FALSE;\r
57 }\r
58 \r
59 BOOLEAN\r
60 getChan_nowait(Chan c, char *data, size_t size) {\r
61   if (ringb_read_space(c->rb) >= size) {\r
62     ringb_read(c->rb, data, size);\r
63     return TRUE;\r
64   } else return FALSE;\r
65 }\r
66 \r
67 Chan\r
68 openChan(char *path,size_t want) {\r
69   Chan c = (Chan) safealloc(sizeof(ChanDesc), 1, "Chan header");\r
70   c->size.rib = sizeof(ringb_t);\r
71   c->size.buf = (size_t) nblock2((int) want);\r
72 #ifndef _WINDOWS\r
73   c->file.path = path;\r
74   if ((c->file.desc = open(c->file.path, O_RDWR)) == -1) {\r
75     fprintf(stderr, "can't open Chan file %s\n", c->file.path);\r
76     exit(1);\r
77   }\r
78   c->size.tot = c->size.rib + c->size.buf;\r
79   if ((c->size.tru = fdsize(c->file.desc)) < c->size.tot) {\r
80     fprintf(stderr,\r
81             "Chan file %s is too small (%d) for required size (%s)\n",\r
82             c->file.path, c->size.tru, c->size.tot);\r
83     exit(1);\r
84   }\r
85   if (!(c->map.base = (char *) mmap(0,\r
86                                     c->size.tot,\r
87                                     PROT_READ | PROT_WRITE,\r
88                                     MAP_SHARED,\r
89                                     c->file.desc,\r
90                                     0))) {\r
91     fprintf(stderr, "can't memory map %s for Chan\n",\r
92             c->file.path);\r
93     exit(1);\r
94   }\r
95 #endif\r
96   if (!(c->rb = ringb_create(c->size.buf))) {\r
97     fprintf(stderr, "can't make RB for Chan\n");\r
98     exit(1);\r
99   }\r
100   return c;\r
101 }\r
102 \r
103 void\r
104 closeChan(Chan c) {\r
105   if (c) {\r
106 #ifndef _WINDOWS\r
107     munmap(c->map.base, c->size.tot);\r
108     close(c->file.desc);\r
109 #endif\r
110     ringb_destroy(c->rb);\r
111     safefree((char *) c);\r
112   }\r
113 }\r