Contiki 2.5
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  * $Id: arg.c,v 1.2 2006/08/14 23:39:23 oliverschmidt Exp $
64  *
65  */
66 
67 #include "contiki.h"
68 #include "sys/arg.h"
69 
70 /**
71  * \internal Structure used for holding an argument buffer.
72  */
73 struct argbuf {
74  char buf[128];
75  char used;
76 };
77 
78 static struct argbuf bufs[1];
79 
80 /*-----------------------------------------------------------------------------------*/
81 /**
82  * \internal Initalizer, called by the dispatcher module.
83  */
84 /*-----------------------------------------------------------------------------------*/
85 void
86 arg_init(void)
87 {
88  bufs[0].used = 0;
89 }
90 /*-----------------------------------------------------------------------------------*/
91 /**
92  * Allocates an argument buffer.
93  *
94  * \param size The requested size of the buffer, in bytes.
95  *
96  * \return Pointer to allocated buffer, or NULL if no buffer could be
97  * allocated.
98  *
99  * \note It currently is not possible to allocate argument buffers of
100  * any other size than 128 bytes.
101  *
102  */
103 /*-----------------------------------------------------------------------------------*/
104 char *
105 arg_alloc(char size)
106 {
107  if(bufs[0].used == 0) {
108  bufs[0].used = 1;
109  return bufs[0].buf;
110  }
111  return 0;
112 }
113 /*-----------------------------------------------------------------------------------*/
114 /**
115  * Deallocates an argument buffer.
116  *
117  * This function deallocates the argument buffer pointed to by the
118  * parameter, but only if the buffer actually is an argument buffer
119  * and is allocated. It is perfectly safe to call this function with
120  * any pointer.
121  *
122  * \param arg A pointer.
123  */
124 /*-----------------------------------------------------------------------------------*/
125 void
126 arg_free(char *arg)
127 {
128  if(arg == bufs[0].buf) {
129  bufs[0].used = 0;
130  }
131 }
132 /*-----------------------------------------------------------------------------------*/
133 /** @} */
134 /** @} */