<br clear="all">Hi all,<br>   I added some printf message into malloc.c, and find that the function setup() which used to init the memory seems not executed very well. <br>I put my own malloc.c and the log attached to this mail. Hope some one can help me to find out the problems.<br>
<br>static void setup(void)<br>{<br>    int size = (unsigned int)(&_eheap - &_heap) - HDRSIZE;<br><br>    *((hdrtype_t *) hstart) = FREE_BLOCK(size);<br>    <span style="color: rgb(255, 0, 0);">printf("%s the memory size:0x%x,begin:0x%x end:0x%x, hstart:0x%x\n",__func__,size,(unsigned int)&_heap,(unsigned int)&_eheap,(unsigned int )*((hdrtype_t *) hstart));</span><br style="color: rgb(255, 0, 0);">
<br><br>}<br><br>static void *alloc(int len)<br>{<br>  <span style="color: rgb(255, 0, 0);">  hdrtype_t header;</span><br style="color: rgb(255, 0, 0);"><span style="color: rgb(255, 0, 0);">    void *ptr = hstart;</span><br style="color: rgb(255, 0, 0);">
<span style="color: rgb(255, 0, 0);">    printf("%s length=0x%x, the ptr=0x%x\n",__func__,len,(unsigned int )*((hdrtype_t*)ptr));</span><br style="color: rgb(255, 0, 0);"><span style="color: rgb(255, 0, 0);">    printf("%s the memory begin:0x%x end:0x%x, hstart:0x%x\n",__func__,(unsigned int)&_heap,(unsigned int)&_eheap,(unsigned int )*((hdrtype_t *) hstart));</span><br style="color: rgb(255, 0, 0);">
<span style="color: rgb(255, 0, 0);">    /* Align the size. */</span><br style="color: rgb(255, 0, 0);"><span style="color: rgb(255, 0, 0);">    len = (len + 3) & ~3;</span><br style="color: rgb(255, 0, 0);"><br style="color: rgb(255, 0, 0);">
<span style="color: rgb(255, 0, 0);">    if (!len || len > 0xffffff)</span><br style="color: rgb(255, 0, 0);"><span style="color: rgb(255, 0, 0);">        return (void *)NULL;</span><br style="color: rgb(255, 0, 0);"><br style="color: rgb(255, 0, 0);">
<span style="color: rgb(255, 0, 0);">    /* Make sure the region is setup correctly. */</span><br style="color: rgb(255, 0, 0);"><span style="color: rgb(255, 0, 0);">    if (!HAS_MAGIC(*((hdrtype_t *) ptr)))</span><br style="color: rgb(255, 0, 0);">
<span style="color: rgb(255, 0, 0);">        setup();</span><br style="color: rgb(255, 0, 0);"><span style="color: rgb(255, 0, 0);">    if (!HAS_MAGIC(*((hdrtype_t *) ptr))){</span><br style="color: rgb(255, 0, 0);"><span style="color: rgb(255, 0, 0);">        printf("set up failed,ptr=0x%x\n",(unsigned int)*((hdrtype_t *) ptr));</span><br style="color: rgb(255, 0, 0);">
<span style="color: rgb(255, 0, 0);">    }</span><br style="color: rgb(255, 0, 0);"><span style="color: rgb(255, 0, 0);">    /* Find some free space. */</span><br style="color: rgb(255, 0, 0);"><span style="color: rgb(255, 0, 0);">    do {</span><br style="color: rgb(255, 0, 0);">
<span style="color: rgb(255, 0, 0);">        header = *((hdrtype_t *) ptr);</span><br style="color: rgb(255, 0, 0);"><span style="color: rgb(255, 0, 0);">        int size = SIZE(header);</span><br style="color: rgb(255, 0, 0);">
<br style="color: rgb(255, 0, 0);"><span style="color: rgb(255, 0, 0);">        if (!HAS_MAGIC(header) || size == 0) {</span><br style="color: rgb(255, 0, 0);"><span style="color: rgb(255, 0, 0);">            printf("memory allocator panic!!! the size=0x%x. header=0x%x\n",size,(unsigned int)header);</span><br style="color: rgb(255, 0, 0);">
<span style="color: rgb(255, 0, 0);">            halt();</span><br>        }<br><br>        if (header & FLAG_FREE) {<br>            if (len <= size) {<br>                void *nptr = ptr + (HDRSIZE + len);<br>                int nsize = size - (HDRSIZE + len);<br>
<br>                /* If there is still room in this block,<br>                 * then mark it as such otherwise account<br>                 * the whole space for that block.<br>                 */<br><br>                if (nsize > 0) {<br>
                    /* Mark the block as used. */<br>                    *((hdrtype_t *) ptr) = USED_BLOCK(len);<br><br>                    /* Create a new free block. */<br>                    *((hdrtype_t *) nptr) =<br>
                        FREE_BLOCK(nsize);<br>                } else {<br>                    /* Mark the block as used. */<br>                    *((hdrtype_t *) ptr) = USED_BLOCK(size);<br>                }<br><br>                return (void *)(ptr + HDRSIZE);<br>
            }<br>        }<br><br>        ptr += HDRSIZE + size;<br><br>    } while (ptr < hend);<br><br>    /* Nothing available. */<br>    return (void *)NULL;<br>}<br>log:<br>Attempting to init PCI bdf 06:05.0 (dev/ven 30381106)<br>
Copying option rom (size 130560) from 0xfff00000 to ce000<br>Checking rom 0x000ce000 (sig aa55 size 255)<br>Running option rom at ce00:0003<br>hello, initialize_usb<br>00:13.0 4387:1002.0 OHCI controller<br>Not supported.<br>
00:13.1 4388:1002.1 OHCI controller<br>Not supported.<br>00:13.2 4389:1002.2 OHCI controller<br>Not supported.<br>00:13.3 438a:1002.3 OHCI controller<br>Not supported.<br>00:13.4 438b:1002.4 OHCI controller<br>Not supported.<br>
00:13.5 4386:1002.5 EHCI controller<br>Not supported.<br>00:05.0 3038:1106.0 UHCI controller<br><span style="color: rgb(255, 0, 0);">alloc length=0x238, the ptr=0x0</span><br style="color: rgb(255, 0, 0);"><span style="color: rgb(255, 0, 0);">alloc the memory begin:0x7860 end:0x57860, hstart:0x0</span><br style="color: rgb(255, 0, 0);">
<span style="color: rgb(255, 0, 0);">setup the memory size:0x4fffc,begin:0x7860 end:0x57860, hstart:0xaa04fffc</span><br style="color: rgb(255, 0, 0);"><span style="color: rgb(255, 0, 0);">set up failed,ptr=0x0</span><br style="color: rgb(255, 0, 0);">
<span style="color: rgb(255, 0, 0);">memory allocator panic!!! the size=0x0. header=0x0</span><br><br>-- <br>Jason Wang <br>Peking University<br>