Skip to content

Commit

Permalink
Merge pull request #18 from sandlbn/OS4
Browse files Browse the repository at this point in the history
Removed Mem Allocation optimization #17
  • Loading branch information
sandlbn authored Nov 26, 2024
2 parents 0319fab + 8755951 commit 4b2ec35
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 56 deletions.
2 changes: 1 addition & 1 deletion include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#define INITIAL_BUFFER_SIZE (64 * 1024) // 64 kb :)
#define MIN_BUFFER_SIZE (512 * 1024) // 512KB minimum
#define MAX_BUFFER_SIZE (64 * 1024 * 1024) // 4MB maximum
#define PREFERRED_BUFFER_SIZE (8 * 1024 * 1024) // 8MB preferred
#define PREFERRED_BUFFER_SIZE (2 * 1024 * 1024) // 2MB preferred
#define READ_CHUNK_SIZE (8 * 1024) // Read 8KB at a time
#define MAX_STATUS_MSG_LEN 256
#define PLS_HEADER "[playlist]\n"
Expand Down
2 changes: 0 additions & 2 deletions include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,4 @@ void UpdateStatusMessage(const char *message);
void SanitizeAmigaFilename(const char *input, char *output, size_t maxLen);
BOOL EnsureSettingsPath(void);
void cleanNonAscii(char *dst, const char *src, size_t maxLen);
void *AllocateResponseBuffer(void);
ULONG GetOptimalBufferSize(void);
#endif /* UTILS_H */
2 changes: 1 addition & 1 deletion src/network/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ char* make_http_request(const struct APISettings *settings, const char *path) {
char msg[MAX_STATUS_MSG_LEN];
int chunk_count;

buffer_size = GetOptimalBufferSize();
buffer_size = PREFERRED_BUFFER_SIZE;
DEBUG("Initial buffer size: %ld bytes", buffer_size);

sockfd = socket(AF_INET, SOCK_STREAM, 0);
Expand Down
52 changes: 0 additions & 52 deletions src/utils/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,55 +223,3 @@ void cleanNonAscii(char *dst, const char *src, size_t maxLen)
strcpy(dst, "Unknown");
}
}

// Function to determine optimal buffer size
ULONG GetOptimalBufferSize(void) {
struct MemHeader *mh;
ULONG largest = 0;
ULONG available = 0;

// Check available memory
Forbid();
for (mh = (struct MemHeader *)SysBase->MemList.lh_Head;
mh->mh_Node.ln_Succ;
mh = (struct MemHeader *)mh->mh_Node.ln_Succ) {
available += mh->mh_Free;
if (mh->mh_Free > largest) {
largest = mh->mh_Free;
}
}
Permit();

// Calculate buffer size based on available memory
ULONG bufferSize;

if (available > 8 * 1024 * 1024) { // More than 8MB free
bufferSize = PREFERRED_BUFFER_SIZE;
} else if (available > 4 * 1024 * 1024) { // More than 4MB free
bufferSize = PREFERRED_BUFFER_SIZE / 2;
} else {
bufferSize = MIN_BUFFER_SIZE;
}

// Ensure we stay within limits
if (bufferSize > MAX_BUFFER_SIZE) bufferSize = MAX_BUFFER_SIZE;
if (bufferSize < MIN_BUFFER_SIZE) bufferSize = MIN_BUFFER_SIZE;

// Ensure we don't try to allocate more than 75% of largest free block
ULONG maxSafe = (largest * 3) / 4;
if (bufferSize > maxSafe) bufferSize = maxSafe;

DEBUG("Memory available: %ld bytes, Selected buffer size: %ld bytes", available, bufferSize);
return bufferSize;
}

void *AllocateResponseBuffer(void) {
ULONG bufferSize = GetOptimalBufferSize();
void *buffer = AllocVec(bufferSize, MEMF_CLEAR);
if (!buffer) {
// If allocation failed, try minimum size
bufferSize = MIN_BUFFER_SIZE;
buffer = AllocVec(bufferSize, MEMF_CLEAR);
}
return buffer;
}

0 comments on commit 4b2ec35

Please sign in to comment.