@@ -251,17 +251,42 @@ int aws_mmap_context_unmap_content(void *mapped_addr, size_t len) {
251251 if (!mapped_addr ) {
252252 return AWS_OP_SUCCESS ;
253253 }
254- if (munmap (mapped_addr , len )) {
255- /**
256- * Remove any mappings for those entire pages containing any part of the address space of the process starting
257- * at addr and continuing for len bytes.
258- *
259- * So, even if the len is not the exact match of the length of bytes we mapped, we will still free the number of
260- * pages we allocated.
261- **/
262- int error_code = errno ;
263- AWS_LOGF_ERROR (AWS_LS_S3_GENERAL , "Failed to unmap memory with error code %d" , error_code );
264- return aws_translate_and_raise_io_error (error_code );
254+ long page_size = sysconf (_SC_PAGE_SIZE );
255+ if (len > page_size ) {
256+ size_t repeat = len / page_size ;
257+ size_t reminder = len % page_size ;
258+ for (size_t i = 0 ; i < repeat ; i ++ ) {
259+ if (munmap (mapped_addr + (i * page_size ), page_size )) {
260+ int error_code = errno ;
261+ AWS_LOGF_ERROR (AWS_LS_S3_GENERAL , "Failed to unmap memory with error code %d" , error_code );
262+ return aws_translate_and_raise_io_error (error_code );
263+ }
264+ }
265+ if (reminder && (mapped_addr + repeat * page_size , reminder )) {
266+ /**
267+ * Remove any mappings for those entire pages containing any part of the address space of the process
268+ *starting at addr and continuing for len bytes.
269+ *
270+ * So, even if the len is not the exact match of the length of bytes we mapped, we will still free the
271+ *number of pages we allocated.
272+ **/
273+ int error_code = errno ;
274+ AWS_LOGF_ERROR (AWS_LS_S3_GENERAL , "Failed to unmap memory with error code %d" , error_code );
275+ return aws_translate_and_raise_io_error (error_code );
276+ }
277+ } else {
278+ if (munmap (mapped_addr , len )) {
279+ /**
280+ * Remove any mappings for those entire pages containing any part of the address space of the process
281+ *starting at addr and continuing for len bytes.
282+ *
283+ * So, even if the len is not the exact match of the length of bytes we mapped, we will still free the
284+ *number of pages we allocated.
285+ **/
286+ int error_code = errno ;
287+ AWS_LOGF_ERROR (AWS_LS_S3_GENERAL , "Failed to unmap memory with error code %d" , error_code );
288+ return aws_translate_and_raise_io_error (error_code );
289+ }
265290 }
266291 return AWS_OP_SUCCESS ;
267292}
0 commit comments