~ubuntu-branches/ubuntu/saucy/avr-libc/saucy

« back to all changes in this revision

Viewing changes to libc/stdlib/realloc.c

  • Committer: Bazaar Package Importer
  • Author(s): Hakan Ardo
  • Date: 2011-07-14 11:15:32 UTC
  • mfrom: (1.1.10 upstream) (4.1.6 sid)
  • Revision ID: james.westby@ubuntu.com-20110714111532-e83i3vqdowgxw8lv
Tags: 1:1.7.1-2
include/util/delay.h.in: Add math.h to list of includes (closes:
#633822)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (c) 2004 Joerg Wunsch
 
1
/* Copyright (c) 2004, 2010 Joerg Wunsch
2
2
   All rights reserved.
3
3
 
4
4
   Redistribution and use in source and binary forms, with or without
28
28
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
29
  POSSIBILITY OF SUCH DAMAGE.
30
30
*/
31
 
/* $Id: realloc.c,v 1.4.4.2 2009/04/01 23:12:11 arcanum Exp $ */
 
31
/* $Id: realloc.c 2127 2010-06-07 14:49:37Z joerg_wunsch $ */
32
32
 
33
33
#include <stdlib.h>
34
34
#include <string.h>
60
60
        if (cp < cp1)
61
61
                /* Pointer wrapped across top of RAM, fail. */
62
62
                return 0;
63
 
        fp2 = (struct __freelist *)(cp - sizeof(size_t));
64
63
 
65
64
        /*
66
65
         * See whether we are growing or shrinking.  When shrinking,
75
74
                if (fp1->sz <= sizeof(struct __freelist) ||
76
75
                    len > fp1->sz - sizeof(struct __freelist))
77
76
                        return ptr;
 
77
                fp2 = (struct __freelist *)cp;
78
78
                fp2->sz = fp1->sz - len - sizeof(size_t);
79
79
                fp1->sz = len;
80
80
                free(&(fp2->nx));
87
87
         */
88
88
        incr = len - fp1->sz;
89
89
        cp = (char *)ptr + fp1->sz;
 
90
        fp2 = (struct __freelist *)cp;
90
91
        for (s = 0, ofp3 = 0, fp3 = __flp;
91
92
             fp3;
92
93
             ofp3 = fp3, fp3 = fp3->nx) {
93
 
                if (fp3 == fp2 && fp3->sz >= incr) {
 
94
                if (fp3 == fp2 && fp3->sz + sizeof(size_t) >= incr) {
94
95
                        /* found something that fits */
95
 
                        if (incr <= fp3->sz + sizeof(size_t)) {
 
96
                        if (fp3->sz + sizeof(size_t) - incr > sizeof(struct __freelist)) {
 
97
                                /* split off a new freelist entry */
 
98
                                cp = (char *)ptr + len;
 
99
                                fp2 = (struct __freelist *)cp;
 
100
                                fp2->nx = fp3->nx;
 
101
                                fp2->sz = fp3->sz - incr;
 
102
                                fp1->sz = len;
 
103
                        } else {
96
104
                                /* it just fits, so use it entirely */
97
105
                                fp1->sz += fp3->sz + sizeof(size_t);
98
 
                                if (ofp3)
99
 
                                        ofp3->nx = fp3->nx;
100
 
                                else
101
 
                                        __flp = fp3->nx;
102
 
                                return ptr;
 
106
                                fp2 = fp3->nx;
103
107
                        }
104
 
                        /* split off a new freelist entry */
105
 
                        cp = (char *)ptr + len;
106
 
                        fp2 = (struct __freelist *)(cp - sizeof(size_t));
107
 
                        fp2->nx = fp3->nx;
108
 
                        fp2->sz = fp3->sz - incr - sizeof(size_t);
109
108
                        if (ofp3)
110
109
                                ofp3->nx = fp2;
111
110
                        else
112
111
                                __flp = fp2;
113
 
                        fp1->sz = len;
114
112
                        return ptr;
115
113
                }
116
114
                /*