笑含风的个人空间 https://blog.eetop.cn/miaofng [收藏] [复制] [分享] [RSS]

空间首页 动态 记录 日志 相册 主题 分享 留言板 个人资料

日志

关于 arm-linux- 编译 u-boot-1.3.0

已有 2774 次阅读| 2007-11-24 23:33

天气: 晴朗
心情: 高兴

笑含风 2007-11-24

最近在使用ELDK(arm-linux-) u-boot 1.3.0 时候遇到到网上盛传的 malloc 返回指针为0的怪问题

(DL)malloc 调用流程为:

->malloc() //dlmalloc.c

->mALLOc() //dlmalloc.c

->malloc_extend_top() //dlmalloc.c

->sbrk() //board.c ----->mem_malloc_init() -------> CFG_MALLOC_LEN

即首次使用malloc时堆中的可用缓冲区字节数为0. 然后根据DLMALLOC 的算法会通过sbrk函数去往堆中添加内存(最多CFG_MALLOC_LEN bytes)。调试记录如下:

nf: malloc() nb=65544,bytes=65536
nf:remainder_size = -65544 bytes
nf:chunksize(top) = 0 bytes
nf:nb = 65544 bytes
nf: this is func malloc_extend_top()
nb = 65544
top_pad = 6291456 !!!!!!!!!!error
MINSIZE = 16
sbrk_size = 6357016
sbrk_size = 6357016
nf: calling sbrk(sbrk_size = 6357016 bytes)
nf:this is func sbrk() start, increment = 6357016 bytes
nf: sbrk return fail, 55555~~~~~
nf:this is func sbrk() start, increment = 4072 bytes
nf: malloc fail:(

从调试记录中可见top_pad 值异常,top_pad的声明以及解释如下:

static unsigned long top_pad          = DEFAULT_TOP_PAD; // =0
/*
    M_TOP_PAD is the amount of extra `padding' space to allocate or
      retain whenever sbrk is called. It is used in two ways internally:

      * When sbrk is called to extend the top of the arena to satisfy
 a new malloc request, this much padding is added to the sbrk
 request.

      * When malloc_trim is called automatically from free(),
 it is used as the `pad' argument.

      In both cases, the actual amount of padding is rounded
      so that the end of the arena is always a system page boundary.

      The main reason for using padding is to avoid calling sbrk so
      often. Having even a small pad greatly reduces the likelihood
      that nearly every malloc request during program start-up (or
      after trimming) will invoke sbrk, which needlessly wastes
      time.

      Automatic rounding-up to page-size units is normally sufficient
      to avoid measurable overhead, so the default is 0.  However, in
      systems where sbrk is relatively slow, it can pay to increase
      this value, at the expense of carrying around more memory than
      the program needs.

*/

可见top_pad赋初值为0, 在malloc_extend_top() 中调用到的值却为6291456从而导致内存分配失败。尝试着仅仅将此值改为1024,1,2 等非零值则一切正常,真是奇怪!!!

debug 记录如下(top_pad = 1024):

nf: malloc() nb=65544,bytes=65536
nf:remainder_size = -65544 bytes
nf:chunksize(top) = 0 bytes
nf:nb = 65544 bytes
nf: this is func malloc_extend_top()
nb = 65544
top_pad = 1024 !!!correct
MINSIZE = 16
sbrk_size = 66584
sbrk_size = 66584
nf: calling sbrk(sbrk_size = 66584 bytes)
nf:this is func sbrk() start, increment = 66584 bytes
nf:this is func sbrk() start, increment = 3048 bytes

结论: 在使用denx 自带的 arm-linux-gcc 编译 u-boot 时候将dlmalloc.c 中 top_pad 赋值改成1024(或其他非0值)即可

避免malloc分配内存失败的问题。如下:

static unsigned long top_pad          = 1024;//DEFAULT_TOP_PAD; // =0

但是此问题发生的root cause还需高手指点,本文只期望能够抛砖引玉^^


点赞

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 注册

  • 关注TA
  • 加好友
  • 联系TA
  • 0

    周排名
  • 0

    月排名
  • 0

    总排名
  • 0

    关注
  • 1

    粉丝
  • 0

    好友
  • 0

    获赞
  • 5

    评论
  • 398

    访问数
关闭

站长推荐 上一条 /1 下一条

小黑屋| 关于我们| 联系我们| 在线咨询| 隐私声明| EETOP 创芯网
( 京ICP备:10050787号 京公网安备:11010502037710 )

GMT+8, 2024-5-4 01:53 , Processed in 0.018269 second(s), 13 queries , Gzip On, Redis On.

eetop公众号 创芯大讲堂 创芯人才网
返回顶部