EVE 1.0
arg.c
Go to the documentation of this file.
1 /**
2  * \file
3  * Argument buffer for passing arguments when starting processes
4  * \author Adam Dunkels <adam@dunkels.com>
5  */
6 
7 /**
8  * \addtogroup sys
9  * @{
10  */
11 
12 /**
13  * \defgroup arg Argument buffer
14  * @{
15  *
16  * The argument buffer can be used when passing an argument from an
17  * exiting process to a process that has not been created yet. Since
18  * the exiting process will have exited when the new process is
19  * started, the argument cannot be passed in any of the processes'
20  * addres spaces. In such situations, the argument buffer can be used.
21  *
22  * The argument buffer is statically allocated in memory and is
23  * globally accessible to all processes.
24  *
25  * An argument buffer is allocated with the arg_alloc() function and
26  * deallocated with the arg_free() function. The arg_free() function
27  * is designed so that it can take any pointer, not just an argument
28  * buffer pointer. If the pointer to arg_free() is not an argument
29  * buffer, the function does nothing.
30  */
31 
32 /*
33  * Copyright (c) 2003, Adam Dunkels.
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  * notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above
42  * copyright notice, this list of conditions and the following
43  * disclaimer in the documentation and/or other materials provided
44  * with the distribution.
45  * 3. The name of the author may not be used to endorse or promote
46  * products derived from this software without specific prior
47  * written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
50  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
51  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
53  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
55  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
56  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
57  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
58  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
59  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60  *
61  * This file is part of the Contiki desktop OS
62  *
63  *
64  */
65 
66 #include "contiki.h"
67 #include "sys/arg.h"
68 
69 /**
70  * \internal Structure used for holding an argument buffer.
71  */
72 struct argbuf {
73  char buf[128];
74  char used;
75 };
76 
77 static struct argbuf bufs[1];
78 
79 /*-----------------------------------------------------------------------------------*/
80 /**
81  * \internal Initalizer, called by the dispatcher module.
82  */
83 /*-----------------------------------------------------------------------------------*/
84 void
85 arg_init(void)
86 {
87  bufs[0].used = 0;
88 }
89 /*-----------------------------------------------------------------------------------*/
90 /**
91  * Allocates an argument buffer.
92  *
93  * \param size The requested size of the buffer, in bytes.
94  *
95  * \return Pointer to allocated buffer, or NULL if no buffer could be
96  * allocated.
97  *
98  * \note It currently is not possible to allocate argument buffers of
99  * any other size than 128 bytes.
100  *
101  */
102 /*-----------------------------------------------------------------------------------*/
103 char *
104 arg_alloc(char size)
105 {
106  if(bufs[0].used == 0) {
107  bufs[0].used = 1;
108  return bufs[0].buf;
109  }
110  return 0;
111 }
112 /*-----------------------------------------------------------------------------------*/
113 /**
114  * Deallocates an argument buffer.
115  *
116  * This function deallocates the argument buffer pointed to by the
117  * parameter, but only if the buffer actually is an argument buffer
118  * and is allocated. It is perfectly safe to call this function with
119  * any pointer.
120  *
121  * \param arg A pointer.
122  */
123 /*-----------------------------------------------------------------------------------*/
124 void
125 arg_free(char *arg)
126 {
127  if(arg == bufs[0].buf) {
128  bufs[0].used = 0;
129  }
130 }
131 /*-----------------------------------------------------------------------------------*/
132 /** @} */
133 /** @} */
char * arg_alloc(char size)
Definition: arg.c:104
void arg_free(char *arg)
Definition: arg.c:125