]> rtime.felk.cvut.cz Git - arc.git/blob - common/mbox.c
Merge branch 'mikulka' of git@rtime.felk.cvut.cz:arc into mikulka
[arc.git] / common / mbox.c
1 /* -------------------------------- Arctic Core ------------------------------\r
2  * Arctic Core - the open source AUTOSAR platform http://arccore.com\r
3  *\r
4  * Copyright (C) 2009  ArcCore AB <contact@arccore.com>\r
5  *\r
6  * This source code is free software; you can redistribute it and/or modify it\r
7  * under the terms of the GNU General Public License version 2 as published by the\r
8  * Free Software Foundation; See <http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt>.\r
9  *\r
10  * This program is distributed in the hope that it will be useful, but\r
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License\r
13  * for more details.\r
14  * -------------------------------- Arctic Core ------------------------------*/\r
15 /*\r
16  * A message box implementation.\r
17  *\r
18  * Notes:\r
19  * - Uses heap\r
20  * - Do NOT use Arc_MBoxDestroy() call, it's there for testing purposes only.
21  */\r
22 \r
23 //#define _TEST_MBOX_\r
24 \r
25 #include "mbox.h"\r
26 #include <stdlib.h>\r
27 #include <string.h>\r
28 #include <stddef.h>\r
29 #include <stdint.h>\r
30 \r
31 \r
32 #ifdef _TEST_MBOX_\r
33 #include <stdio.h>\r
34 #include <assert.h>\r
35 #endif\r
36 \r
37 \r
38 \r
39 Arc_MBoxType* Arc_MBoxCreate( size_t size ) {\r
40         Arc_MBoxType *mPtr;\r
41 \r
42         mPtr = malloc(sizeof(Arc_MBoxType));\r
43         mPtr->cirqPtr = CirqBuffDynCreate(size,sizeof(void *));\r
44 \r
45         return mPtr;\r
46 }\r
47 \r
48 \r
49 void Arc_MBoxDestroy( Arc_MBoxType *mPtr ) {\r
50         CirqBuffDynDestroy(mPtr->cirqPtr);\r
51         free(mPtr);\r
52 }\r
53 \r
54 /**\r
55  * Post a message to a box, non-blocking.\r
56  *
57  */\r
58 int Arc_MBoxPost( Arc_MBoxType *mPtr, void *msg ) {\r
59         int rv;\r
60         rv = CirqBuffPush(mPtr->cirqPtr,msg);\r
61         if( rv != 0) {\r
62                 return 1;\r
63         }\r
64 \r
65         return 0;\r
66 }\r
67 \r
68 /**\r
69  *
70  */\r
71 int Arc_MBoxFetch(Arc_MBoxType *mPtr, void *msg)\r
72 {\r
73         int rv;\r
74         rv = CirqBuffPop(mPtr->cirqPtr,msg);\r
75         if(rv != 0) {\r
76                 return 1;\r
77         }\r
78         return 0;\r
79 }\r
80 \r
81 #ifdef _TEST_MBOX_\r
82 int main( void ) {\r
83         Arc_MBoxType *myBoxes[10];\r
84         uint8_t *dataPtr;\r
85 \r
86         printf("Hej\n");\r
87 \r
88         myBoxes[0] = Arc_MBoxCreate(2);\r
89         myBoxes[1] = Arc_MBoxCreate(4);\r
90 \r
91         dataPtr = malloc(10);\r
92         dataPtr[0] = 1;\r
93         Arc_MBoxPost(myBoxes[0],&dataPtr);\r
94 \r
95         dataPtr = malloc(20);\r
96         dataPtr[0] = 2;\r
97         Arc_MBoxPost(myBoxes[0],&dataPtr);\r
98 \r
99         Arc_MBoxFetch(myBoxes[0],&dataPtr);\r
100     assert(dataPtr[0] == 1);\r
101         free(dataPtr);\r
102 \r
103         Arc_MBoxFetch(myBoxes[0],&dataPtr);\r
104         assert(dataPtr[0] == 2);\r
105         free(dataPtr);\r
106 \r
107         Arc_MBoxDestroy(myBoxes[0]);\r
108         Arc_MBoxDestroy(myBoxes[1]);\r
109         return 0;\r
110 }\r
111 \r
112 #endif\r