Skip to content

Commit 7bd8445

Browse files
authored
Merge pull request #2762 from ghaerr/fmemalloc
[kernel,libc] Cleanup fmemalloc, add warning for allocations > 64K in PM
2 parents ca523bf + f19f188 commit 7bd8445

4 files changed

Lines changed: 21 additions & 8 deletions

File tree

elks/arch/i86/mm/malloc.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ segment_s * seg_alloc_fixed (seg_t base, segext_t size, word_t type)
176176

177177
#endif
178178

179-
// Allocate segment, limited to 64K in PM for now
179+
// Allocate segment
180180

181181
segment_s * seg_alloc (segext_t size, word_t type)
182182
{
@@ -386,7 +386,8 @@ int sys_sbrk(int increment, segoff_t *pbrk)
386386
}
387387

388388
// allocate memory for process, return segment
389-
int sys_fmemalloc(int paras, unsigned short *pseg)
389+
// size > 64K require PM-incompatible segment arithmetic or 32-bit instruction prefix
390+
int sys_fmemalloc(unsigned int paras, unsigned short *pseg)
390391
{
391392
segment_s *seg;
392393
int err;
@@ -396,7 +397,11 @@ int sys_fmemalloc(int paras, unsigned short *pseg)
396397
return err;
397398
if (paras == 0)
398399
return -EINVAL;
399-
seg = seg_alloc((segext_t)paras, SEG_FLAG_FDAT);
400+
#ifdef CONFIG_286_PMODE
401+
if (paras > 0x1000)
402+
printk("fmemalloc: warning size %uK > 64K\n", paras >> 6);
403+
#endif
404+
seg = seg_alloc(paras, SEG_FLAG_FDAT);
400405
if (!seg) {
401406
debug_brk("(%P)FMEMALLOC %ld FAIL\n", (unsigned long)paras << 4);
402407
return -ENOMEM;

libc/include/malloc.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ extern unsigned int malloc_arena_thresh; /* mem.c wrapper function */
2929
void __far *fmemalloc(unsigned long size);
3030
int fmemfree(void __far *ptr);
3131

32-
int _fmemalloc(int paras, unsigned short *pseg); /* syscall */
33-
int _fmemfree(unsigned short seg); /* syscall */
32+
int _fmemalloc(unsigned int paras, unsigned short *pseg); /* syscall */
33+
int _fmemfree(unsigned short seg); /* syscall */
3434
#endif
3535

3636
/* usable with all mallocs */

libc/malloc/fmemalloc.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,24 @@ static unsigned long maxsize;
2020
#define MK_FP(seg,off) ((void __far *)((((unsigned long)(seg)) << 16) | \
2121
((unsigned int)(off))))
2222

23+
/*
24+
* Allocate up to 1M-16 bytes from main or XMS memory.
25+
* Requesting > 64K requires PM-incompatible segment arithmetic or 32-bit app code.
26+
*/
2327
void __far *fmemalloc(unsigned long size)
2428
{
2529
unsigned short seg;
26-
unsigned int paras = (unsigned int)((size + 15) >> 4);
2730

31+
size = (size + 15) >> 4;
32+
if (size & 0xFFFF0000) { /* > (1M - 16) paragraphs */
33+
errno = EINVAL;
34+
return -1;
35+
}
2836
#if DEBUG == 1
2937
sysctl(CTL_GET, "malloc.debug", &debug_level);
3038
#endif
3139
debug("(%d)FMEMALLOC(%5lu) ", getpid(), size);
32-
if (_fmemalloc(paras, &seg)) {
40+
if (_fmemalloc((unsigned int)size, &seg)) {
3341
debug("= FAIL\n");
3442
return 0;
3543
}

libc/watcom/syscall/fmemalloc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <malloc.h>
88
#include "watcom/syselks.h"
99

10-
int _fmemalloc( int __paras, unsigned short *__pseg )
10+
int _fmemalloc( unsigned int __paras, unsigned short *__pseg )
1111
{
1212
sys_setseg(__pseg);
1313
syscall_res res = sys_call2( SYS_fmemalloc, __paras, (unsigned)__pseg);

0 commit comments

Comments
 (0)