github.com/andybalholm/brotli@v1.0.6/memory.go (about)

     1  package brotli
     2  
     3  /* Copyright 2016 Google Inc. All Rights Reserved.
     4  
     5     Distributed under MIT license.
     6     See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
     7  */
     8  
     9  /*
    10  Dynamically grows array capacity to at least the requested size
    11  T: data type
    12  A: array
    13  C: capacity
    14  R: requested size
    15  */
    16  func brotli_ensure_capacity_uint8_t(a *[]byte, c *uint, r uint) {
    17  	if *c < r {
    18  		var new_size uint = *c
    19  		if new_size == 0 {
    20  			new_size = r
    21  		}
    22  
    23  		for new_size < r {
    24  			new_size *= 2
    25  		}
    26  
    27  		if cap(*a) < int(new_size) {
    28  			var new_array []byte = make([]byte, new_size)
    29  			if *c != 0 {
    30  				copy(new_array, (*a)[:*c])
    31  			}
    32  
    33  			*a = new_array
    34  		} else {
    35  			*a = (*a)[:new_size]
    36  		}
    37  
    38  		*c = new_size
    39  	}
    40  }
    41  
    42  func brotli_ensure_capacity_uint32_t(a *[]uint32, c *uint, r uint) {
    43  	var new_array []uint32
    44  	if *c < r {
    45  		var new_size uint = *c
    46  		if new_size == 0 {
    47  			new_size = r
    48  		}
    49  
    50  		for new_size < r {
    51  			new_size *= 2
    52  		}
    53  
    54  		if cap(*a) < int(new_size) {
    55  			new_array = make([]uint32, new_size)
    56  			if *c != 0 {
    57  				copy(new_array, (*a)[:*c])
    58  			}
    59  
    60  			*a = new_array
    61  		} else {
    62  			*a = (*a)[:new_size]
    63  		}
    64  		*c = new_size
    65  	}
    66  }