imalloc.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:2k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * c 2001 PPC 64 Team, IBM Corp
  3.  * 
  4.  *      This program is free software; you can redistribute it and/or
  5.  *      modify it under the terms of the GNU General Public License
  6.  *      as published by the Free Software Foundation; either version
  7.  *      2 of the License, or (at your option) any later version.
  8.  */
  9. #include <linux/slab.h>
  10. #include <linux/vmalloc.h>
  11. #include <linux/spinlock.h>
  12. #include <asm/uaccess.h>
  13. #include <asm/pgalloc.h>
  14. rwlock_t imlist_lock = RW_LOCK_UNLOCKED;
  15. struct vm_struct * imlist = NULL;
  16. struct vm_struct *get_im_area(unsigned long size)
  17. {
  18. unsigned long addr;
  19. struct vm_struct **p, *tmp, *area;
  20.   
  21. area = (struct vm_struct *) kmalloc(sizeof(*area), GFP_KERNEL);
  22. if (!area)
  23. return NULL;
  24. addr = IMALLOC_START;
  25. write_lock(&imlist_lock);
  26. for (p = &imlist; (tmp = *p) ; p = &tmp->next) {
  27. if (size + addr < (unsigned long) tmp->addr)
  28. break;
  29. addr = tmp->size + (unsigned long) tmp->addr;
  30. if (addr > IMALLOC_END-size) {
  31. write_unlock(&imlist_lock);
  32. kfree(area);
  33. return NULL;
  34. }
  35. }
  36. area->flags = 0;
  37. area->addr = (void *)addr;
  38. area->size = size;
  39. area->next = *p;
  40. *p = area;
  41. write_unlock(&imlist_lock);
  42. return area;
  43. }
  44. void ifree(void * addr)
  45. {
  46. struct vm_struct **p, *tmp;
  47.   
  48. if (!addr)
  49. return;
  50. if ((PAGE_SIZE-1) & (unsigned long) addr) {
  51. printk(KERN_ERR "Trying to ifree() bad address (%p)n", addr);
  52. return;
  53. }
  54. write_lock(&imlist_lock);
  55. for (p = &imlist ; (tmp = *p) ; p = &tmp->next) {
  56. if (tmp->addr == addr) {
  57. *p = tmp->next;
  58. kfree(tmp);
  59. write_unlock(&imlist_lock);
  60. return;
  61. }
  62. }
  63. write_unlock(&imlist_lock);
  64. printk(KERN_ERR "Trying to ifree() nonexistent area (%p)n", addr);
  65. }