source: soft/giet_vm/giet_libs/stdlib.c @ 437

Last change on this file since 437 was 382, checked in by alain, 10 years ago

Major evolution of the malloc library, to provide two new services:

  • all allocated blocks are aligned, and the size is a power of 2.
  • the remote-malloc() function allows the application to explicitely define the cluster(x,y).

We keep only the close-fit allocation policy.
These services were required to implement the distributed SBT barrier.

File size: 2.1 KB
Line 
1//////////////////////////////////////////////////////////////////////////////////
2// File     : stdlib.c
3// Date     : 05/12/2013
4// Author   : Clément DEVIGNE
5// Copyright (c) UPMC-LIP6
6///////////////////////////////////////////////////////////////////////////////////
7
8#include <stdlib.h>
9
10///////////////////////////////////////////////////////////////////////////////////
11int atoi(char *str)
12///////////////////////////////////////////////////////////////////////////////////
13{
14    int res  = 0; // Initialize result
15    int sign = 1; // Initialize sign as positive
16    int i    = 0; // Initialize index of first digit
17
18    if (str[0] == '-') //If number is negative, then update sign
19    {
20        sign = -1; 
21        i++;           // Also update index of first digit
22    }
23
24    for (; str[i] != '\0'; ++i) // Iterate through all digits and update the result
25    {
26        res = res*10 + str[i] - '0';
27    }
28
29    // Return result with sign
30    return sign*res;
31}
32       
33////////////////////////////////////////////////////////////////////////////////////////
34void * memcpy(void *_dst, const void * _src, unsigned int size) 
35////////////////////////////////////////////////////////////////////////////////////////
36{
37    unsigned int * dst = _dst;
38    const unsigned int * src = _src;
39    if (!((unsigned int) dst & 3) && !((unsigned int) src & 3) )
40    {
41        while (size > 3) 
42        {
43            *dst++ = *src++;
44            size -= 4;
45        }
46    }
47
48    unsigned char *cdst = (unsigned char*)dst;
49    unsigned char *csrc = (unsigned char*)src;
50
51    while (size--) 
52    {
53        *cdst++ = *csrc++;
54    }
55    return _dst;
56}
57
58////////////////////////////////////////////////////////////////////////////////////////
59inline void * memset(void * dst, int s, unsigned int size) 
60////////////////////////////////////////////////////////////////////////////////////////
61{
62    char * a = (char *) dst;
63    while (size--)
64    {
65        *a++ = (char)s;
66    }
67    return dst;
68}
69
70// Local Variables:
71// tab-width: 4
72// c-basic-offset: 4
73// c-file-offsets:((innamespace . 0)(inline-open . 0))
74// indent-tabs-mode: nil
75// End:
76// vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
Note: See TracBrowser for help on using the repository browser.