Main Page | Modules | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Examples

arg.c

Go to the documentation of this file.
00001 /**
00002  * \file
00003  * Argument buffer for passing arguments when starting processes
00004  * \author Adam Dunkels <adam@dunkels.com>
00005  */
00006 
00007 /**
00008  * \addtogroup sys
00009  * @{
00010  */
00011 
00012 /**
00013  * \defgroup arg Argument buffer
00014  * @{
00015  *
00016  * The argument buffer can be used when passing an argument from an
00017  * exiting process to a process that has not been created yet. Since
00018  * the exiting process will have exited when the new process is
00019  * started, the argument cannot be passed in any of the processes'
00020  * addres spaces. In such situations, the argument buffer can be used.
00021  *
00022  * The argument buffer is statically allocated in memory and is
00023  * globally accessible to all processes.
00024  *
00025  * An argument buffer is allocated with the arg_alloc() function and
00026  * deallocated with the arg_free() function. The arg_free() function
00027  * is designed so that it can take any pointer, not just an argument
00028  * buffer pointer. If the pointer to arg_free() is not an argument
00029  * buffer, the function does nothing.
00030  */
00031 
00032 /*
00033  * Copyright (c) 2003, Adam Dunkels.
00034  * All rights reserved.
00035  *
00036  * Redistribution and use in source and binary forms, with or without
00037  * modification, are permitted provided that the following conditions
00038  * are met:
00039  * 1. Redistributions of source code must retain the above copyright
00040  *    notice, this list of conditions and the following disclaimer.
00041  * 2. Redistributions in binary form must reproduce the above
00042  *    copyright notice, this list of conditions and the following
00043  *    disclaimer in the documentation and/or other materials provided
00044  *    with the distribution.
00045  * 3. The name of the author may not be used to endorse or promote
00046  *    products derived from this software without specific prior
00047  *    written permission.
00048  *
00049  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
00050  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00051  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00052  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00053  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00054  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00055  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00056  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00057  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00058  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00059  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00060  *
00061  * This file is part of the Contiki desktop OS
00062  *
00063  * $Id: arg.c,v 1.1 2006/06/17 22:41:19 adamdunkels Exp $
00064  *
00065  */
00066 
00067 #include "sys/arg.h"
00068 
00069 /**
00070  * \internal Structure used for holding an argument buffer.
00071  */
00072 struct argbuf {
00073   char buf[128];
00074   char used;
00075 };
00076 
00077 static struct argbuf bufs[1];
00078 
00079 /*-----------------------------------------------------------------------------------*/
00080 /**
00081  * \internal Initalizer, called by the dispatcher module.
00082  */
00083 /*-----------------------------------------------------------------------------------*/
00084 void
00085 arg_init(void)
00086 {
00087   bufs[0].used = 0;
00088 }
00089 /*-----------------------------------------------------------------------------------*/
00090 /**
00091  * Allocates an argument buffer.
00092  *
00093  * \param size The requested size of the buffer, in bytes.
00094  *
00095  * \return Pointer to allocated buffer, or NULL if no buffer could be
00096  * allocated.
00097  *
00098  * \note It currently is not possible to allocate argument buffers of
00099  * any other size than 128 bytes.
00100  *
00101  */
00102 /*-----------------------------------------------------------------------------------*/
00103 char *
00104 arg_alloc(char size)
00105 {
00106   if(bufs[0].used == 0) {
00107     bufs[0].used = 1;
00108     return bufs[0].buf;
00109   }
00110   return 0;
00111 }
00112 /*-----------------------------------------------------------------------------------*/
00113 /**
00114  * Deallocates an argument buffer.
00115  *
00116  * This function deallocates the argument buffer pointed to by the
00117  * parameter, but only if the buffer actually is an argument buffer
00118  * and is allocated. It is perfectly safe to call this function with
00119  * any pointer.
00120  *
00121  * \param arg A pointer.
00122  */
00123 /*-----------------------------------------------------------------------------------*/
00124 void
00125 arg_free(char *arg)
00126 {
00127   if(arg == bufs[0].buf) {
00128     bufs[0].used = 0;
00129   }
00130 }
00131 /*-----------------------------------------------------------------------------------*/
00132 /** @} */
00133 /** @} */

Generated on Thu Jun 22 17:45:43 2006 for Contiki 2.x by  doxygen 1.4.4