Contiki 3.x
arg.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2003, Adam Dunkels.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following
12  * disclaimer in the documentation and/or other materials provided
13  * with the distribution.
14  * 3. The name of the author may not be used to endorse or promote
15  * products derived from this software without specific prior
16  * written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * This file is part of the Contiki desktop OS
31  *
32  *
33  */
34 
35 /**
36  * \file
37  * Argument buffer for passing arguments when starting processes
38  * \author Adam Dunkels <adam@dunkels.com>
39  */
40 
41 /**
42  * \addtogroup sys
43  * @{
44  */
45 
46 /**
47  * \defgroup arg Argument buffer
48  * @{
49  *
50  * The argument buffer can be used when passing an argument from an
51  * exiting process to a process that has not been created yet. Since
52  * the exiting process will have exited when the new process is
53  * started, the argument cannot be passed in any of the processes'
54  * addres spaces. In such situations, the argument buffer can be used.
55  *
56  * The argument buffer is statically allocated in memory and is
57  * globally accessible to all processes.
58  *
59  * An argument buffer is allocated with the arg_alloc() function and
60  * deallocated with the arg_free() function. The arg_free() function
61  * is designed so that it can take any pointer, not just an argument
62  * buffer pointer. If the pointer to arg_free() is not an argument
63  * buffer, the function does nothing.
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)
Allocates an argument buffer.
Definition: arg.c:104
void arg_free(char *arg)
Deallocates an argument buffer.
Definition: arg.c:125