/*
** menmory management module
* [email protected]**/
#include #include #include #define mm_ok (0x00)
#define mm_ng (0xff)
#define mm_unit ((unsigned int)4) /* least malloc size */
#define mm_split_num ((unsigned int)10) /* table len */
#define mm_max_once_malloc ((unsigned int)((mm_unit << mm_split_num) - sizeof(mem_blk_t)) ) /* 4k - 4 */
#define mm_max_unit ((unsigned int)(mm_unit << mm_split_num) ) /* 4k */
#define mm_max_memory ((unsigned int)(mm_max_unit << mm_split_num)) /* 4m */
#define mm_malloc_memory ((unsigned int)(mm_max_memory))
/* print on/off macro */
#ifdef _debug
#define mm_debug(x) x
#else
#define mm_debug(x)
#endif
/* data construct */
typedef union
mem_blk_t;
static int mm_get_unit(unsigned int size);
static unsigned int mm_get_size(unsigned int size);
int mm_chk_merge(void);
mem_blk_t* mm_ctrl[mm_split_num + 1];
int mm_init(mem_blk_t** mm_ctrl)
/* malloc a chunk */
p = malloc(mm_malloc_memory);
if ( null != p)
/* set last null */
mem_blk = (mem_blk_t*)((char*)mem_blk - mm_max_unit);
mem_blk->next = null;
i_ret = mm_ok;
mm_debug(printf("mm:mm_init init ok!!!/n"));
} else
}return i_ret;
}int mm_end(mem_blk_t** mm_ctrl)
else
}return i_ret;
}void* mm_malloc(unsigned int size)
idx = mm_get_unit(size);
if (0 > idx)
return null;
if (null != mm_ctrl[idx])
else
}if (mm_split_num == i_loop)
else
#endif
return null;
}} mm_debug(printf("mm:mm_malloc find split position idx=%08x, splitpos=%08x!!!/n", idx, i_loop));
tmp = i_loop;
mem_blk = mm_ctrl[i_loop];
mm_ctrl[i_loop] = mem_blk->next;
/* split */
p = mem_blk;
msize = mm_get_size(size);
msize_tmp = msize;
for (i_loop = idx; i_loop < tmp ; i_loop++)
// mem_blk->p = (void*)((char*)mem_blk + sizeof(mem_blk_t));
// return (void*)mem_blk->p;
mem_blk->p = (void*)(mm_unit << idx);
p = (void*)((char*)mem_blk + sizeof(mem_blk_t));
mm_debug(printf("mm:mm_malloc ok, address=%08p!!!/n", p));
return p;
} return null;
}int mm_free(void* p)
mem_blk = (mem_blk_t*)((char*)p -sizeof(mem_blk_t));
if (0 >= (unsigned int)mem_blk->p)
mm_debug(printf("mm:mm_free, address=%08p!!!/n", p));
msize = (unsigned int)mem_blk->p;
mem_blk->next = null;
idx = mm_get_unit(msize - sizeof(mem_blk_t));
if (0 > idx)
return mm_ng;
if (null == mm_ctrl[idx])
else
else
p = tmp;
tmp = tmp->next;
}if (null == tmp)
}mm_debug(printf("mm:mm_free ok!!!/n"));
} return mm_ok;
}int main(void)
for (i_loop = 0; i_loop < 1500; i_loop++)
mm_end(mm_ctrl);
return 0;
}int mm_get_unit(unsigned int size)
} if (mm_split_num == i_loop)
return i_loop;
}unsigned int mm_get_size(unsigned int size)
} if (mm_split_num == i_loop)
return (mm_unit << i_loop);
}int mm_chk_merge(void)
自己動手寫驅動程式
一 編碼思維導圖 二 編寫 memdev.c include include include include include struct cdev mdev 1.靜態分配cdev dev t devno int dev1 regs 5 int dev2 regs 5 loff t mem lsee...
自己動手寫作業系統的理由
自己動手寫作業系統的理由 想來想去,是的,我就是這麼想的 1.在學習別人的東西時,有些在搞懂之前好像很神秘,但搞懂之後發現也就那麼回事,往往伴隨而來的是失落。而如果自己動手去設計一些東西時,你一定會在這個過程中收穫創造的成就感。2.目前我的能力只限於單個模組幾千行這個層次,我想進入更高的系統層次。3...
自己動手寫的UART2驅動
昨天發現新唐的bsp中沒有提供uart2的定義 即其操作,只定義了uart0和uart1及其驅動操作。但是涉及的板子中又弄了3路串列埠,當然uart2在其中。第一反應就是查閱新唐的資料,nuc1 的前期設計文件中,有些說可以支援uart2,但是又有很多定義的地方都reserve了,就是本該uart2...