MALLOCX.C 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Substitute malloc functions...
  2. #include <i86.h>
  3. #include <dos.h>
  4. #include <stdio.h>
  5. #include <malloc.h>
  6. #include <string.h>
  7. extern int w95;
  8. struct meminfo {
  9. unsigned LargestBlockAvail;
  10. unsigned MaxUnlockedPage;
  11. unsigned LargestLockablePage;
  12. unsigned LinAddrSpace;
  13. unsigned NumFreePagesAvail;
  14. unsigned NumPhysicalPagesFree;
  15. unsigned TotalPhysicalPages;
  16. unsigned FreeLinAddrSpace;
  17. unsigned SizeOfPageFile;
  18. unsigned Reserved[3];
  19. } MemInfo;
  20. unsigned int get_mem_info()
  21. {
  22. union REGS regs;
  23. struct SREGS sregs;
  24. regs.x.eax=0x500;
  25. memset(&sregs,0,sizeof(sregs));
  26. sregs.es=FP_SEG(&MemInfo);
  27. regs.x.edi=FP_OFF(&MemInfo);
  28. int386x(0x31,&regs,&regs,&sregs);
  29. return (MemInfo.LargestBlockAvail);
  30. }
  31. void *mallocx(unsigned int size)
  32. {
  33. union REGS regs;
  34. unsigned int *address;
  35. unsigned int handle;
  36. if (0)//w95)
  37. return(malloc(size));
  38. if (size==0) return (NULL);
  39. size+=4; // Reserve 4 bytes for handle
  40. regs.x.eax=0x501;
  41. regs.w.bx=size/65536;
  42. regs.w.cx=size%65536;
  43. int386(0x31,&regs,&regs);
  44. if (regs.w.cflag&1)
  45. return (NULL);
  46. address=(unsigned int *)(regs.w.cx+regs.w.bx*65536);
  47. handle=(unsigned int)(regs.w.di+regs.w.si*65536);
  48. *address++=handle;
  49. // printf("Reserve handle = %d\n",handle);
  50. return ((void *)address);
  51. }
  52. void freex(void *address)
  53. {
  54. union REGS regs;
  55. unsigned int handle;
  56. if (0)//w95)
  57. {
  58. free(address);
  59. return;
  60. }
  61. if (address!=NULL)
  62. {
  63. handle=*((unsigned int *)address-1);
  64. // printf("Free handle = %d\n",handle);
  65. regs.x.eax=0x502;
  66. regs.w.si=handle/65536;
  67. regs.w.di=handle%65536;
  68. int386(0x31,&regs,&regs);
  69. // if (regs.w.cflag&1)
  70. // puts("Error freeing linear memory");
  71. }
  72. }
  73.