Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

229 lines
5.5 KiB

  1. /*
  2. * Various allocation routines and routines returning information about
  3. * allocated objects.
  4. */
  5. #include "stevie.h"
  6. char *
  7. alloc(size)
  8. unsigned size;
  9. {
  10. char *p; /* pointer to new storage space */
  11. p = malloc(size);
  12. if ( p == (char *)NULL ) { /* if there is no more room... */
  13. emsg("Insufficient memory");
  14. }
  15. return(p);
  16. }
  17. char *
  18. ralloc(char *block,unsigned newsize)
  19. {
  20. char *p;
  21. if((p = realloc(block,newsize)) == NULL) {
  22. emsg("Insufficient memory");
  23. }
  24. return(p);
  25. }
  26. char *
  27. strsave(string)
  28. char *string;
  29. {
  30. return(strcpy(alloc((unsigned)(strlen(string)+1)),string));
  31. }
  32. void
  33. screenalloc()
  34. {
  35. /*
  36. * If we're changing the size of the screen, free the old arrays
  37. */
  38. if (Realscreen != NULL)
  39. free(Realscreen);
  40. if (Nextscreen != NULL)
  41. free(Nextscreen);
  42. Realscreen = malloc((unsigned)(Rows*Columns));
  43. Nextscreen = malloc((unsigned)(Rows*Columns));
  44. }
  45. /*
  46. * Allocate and initialize a new line structure with room for
  47. * 'nchars'+1 characters. We add one to nchars here to allow for
  48. * null termination because all the callers would just do it otherwise.
  49. */
  50. LINE *
  51. newline(nchars)
  52. int nchars;
  53. {
  54. register LINE *l;
  55. if ((l = (LINE *) alloc(sizeof(LINE))) == NULL)
  56. return (LINE *) NULL;
  57. l->s = alloc((unsigned) (nchars+1)); /* the line is empty */
  58. l->s[0] = NUL;
  59. l->size = nchars + 1;
  60. l->prev = (LINE *) NULL; /* should be initialized by caller */
  61. l->next = (LINE *) NULL;
  62. return l;
  63. }
  64. /*
  65. * filealloc() - construct an initial empty file buffer
  66. */
  67. void
  68. filealloc()
  69. {
  70. if ((Filemem->linep = newline(0)) == NULL) {
  71. fprintf(stderr,"Unable to allocate file memory!\n");
  72. exit(1);
  73. }
  74. if ((Filetop->linep = newline(0)) == NULL) {
  75. fprintf(stderr,"Unable to allocate file memory!\n");
  76. exit(1);
  77. }
  78. if ((Fileend->linep = newline(0)) == NULL) {
  79. fprintf(stderr,"Unable to allocate file memory!\n");
  80. exit(1);
  81. }
  82. Filemem->index = 0;
  83. Filetop->index = 0;
  84. Fileend->index = 0;
  85. Filetop->linep->next = Filemem->linep; /* connect Filetop to Filemem */
  86. Filemem->linep->prev = Filetop->linep;
  87. Filemem->linep->next = Fileend->linep; /* connect Filemem to Fileend */
  88. Fileend->linep->prev = Filemem->linep;
  89. *Curschar = *Filemem;
  90. *Topchar = *Filemem;
  91. Filemem->linep->num = 0;
  92. Fileend->linep->num = 0xffff;
  93. clrall(); /* clear all marks */
  94. u_clear(); /* clear the undo buffer */
  95. }
  96. /*
  97. * freeall() - free the current buffer
  98. *
  99. * Free all lines in the current buffer.
  100. */
  101. void
  102. freeall()
  103. {
  104. register LINE *lp, *xlp;
  105. for (lp = Filetop->linep; lp != NULL ;lp = xlp) {
  106. if (lp->s != NULL)
  107. free(lp->s);
  108. xlp = lp->next;
  109. free((char *)lp);
  110. }
  111. Curschar->linep = NULL; /* clear pointers */
  112. Filetop->linep = NULL;
  113. Filemem->linep = NULL;
  114. Fileend->linep = NULL;
  115. u_clear();
  116. /* _heapmin(); */
  117. }
  118. /*
  119. * bufempty() - return TRUE if the buffer is empty
  120. */
  121. bool_t
  122. bufempty()
  123. {
  124. return (buf1line() && Filemem->linep->s[0] == NUL);
  125. }
  126. /*
  127. * buf1line() - return TRUE if there is only one line
  128. */
  129. bool_t
  130. buf1line()
  131. {
  132. return (Filemem->linep->next == Fileend->linep);
  133. }
  134. /*
  135. * lineempty() - return TRUE if the current line is empty
  136. */
  137. bool_t
  138. lineempty()
  139. {
  140. return (Curschar->linep->s[0] == NUL);
  141. }
  142. /*
  143. * endofline() - return TRUE if the given position is at end of line
  144. *
  145. * This routine will probably never be called with a position resting
  146. * on the NUL byte, but handle it correctly in case it happens.
  147. */
  148. bool_t
  149. endofline(p)
  150. register LNPTR *p;
  151. {
  152. return (p->linep->s[p->index] == NUL || p->linep->s[p->index+1] == NUL);
  153. }
  154. /*
  155. * canincrease(n) - returns TRUE if the current line can be increased 'n' bytes
  156. *
  157. * This routine returns immediately if the requested space is available.
  158. * If not, it attempts to allocate the space and adjust the data structures
  159. * accordingly. If everything fails it returns FALSE.
  160. */
  161. bool_t
  162. canincrease(n)
  163. register int n;
  164. {
  165. register int nsize;
  166. register char *s; /* pointer to new space */
  167. nsize = strlen(Curschar->linep->s) + 1 + n; /* size required */
  168. if (nsize <= Curschar->linep->size)
  169. return TRUE;
  170. /*
  171. * Need to allocate more space for the string. Allow some extra
  172. * space on the assumption that we may need it soon. This avoids
  173. * excessive numbers of calls to malloc while entering new text.
  174. */
  175. if ((s = alloc((unsigned) (nsize + SLOP))) == NULL) {
  176. emsg("Can't add anything, file is too big!");
  177. State = NORMAL;
  178. return FALSE;
  179. }
  180. Curschar->linep->size = nsize + SLOP;
  181. strcpy(s, Curschar->linep->s);
  182. free(Curschar->linep->s);
  183. Curschar->linep->s = s;
  184. return TRUE;
  185. }
  186. char *
  187. mkstr(c)
  188. char c;
  189. {
  190. static char s[2];
  191. s[0] = c;
  192. s[1] = NUL;
  193. return s;
  194. }